English 中文(简体)
Struts Action Validation
原标题:

Hey People. I have been recently validating Struts 2 actions. In One of my action class, the save method to be precise must have two id s. That is an assessmenttype id and a course id. this is what is have so far.

@Test
public void testSave() {
    Assessment assessment = new Assessment();
    assessment.setAssessmentType(assessment.getAssessmentType());
    assessment.setCourse(assessment.getCourse());
    assessment.setAmount(2);
    assessment.setDescription("A test Description");
    assessment.setPercentage(20.0);

    action.setAssessment(assessment);
    action.save();

    assertNotNull(action.getAssessment().getId());
}

all help would be appreciated.

Hey Sorry peeps, i taught i asked the question. But what i am trying to really achive is to the test the action class that i have for assessment. For instance if the client uses the application when adding an assessment, the course id is hidden from the user but to save an assessment it must exist. also to save the assessment they have a select an assessmenttype. therefore for the save method to work perfectly both course id and assessmenttype it must exist.

The question therefore is that i don t really know how to test and implement the course id as well as assessmenttype id.

Thanks again. all help would be appreciated

The Save Action method is described below;

public String save(){
    if (cancel != null) {
        return "cancel";
    }

    boolean isNew = (assessment.getId() == null);

    assessmentType.addAssessment(assessment);
    course.addAssessment(assessment);

    assessment = assessmentManager.save(assessment);


    return "save";

}
问题回答

When unit testing code you need to start with the specification for the method that you are testing. Let us take a look at the method that you are trying to test and "guess" what the specifications are.

  1. If some variable cancel is null then perform the method otherwise return immediately
  2. assessment object cannot be null (otherwise the line boolean isNew = (assessment.getId() == null); will fail and you are not checking for null). On another note this line does not seem to serve any purpose in your code since you do not use isNew anywhere else.
  3. assessmentType is also not null, that is it must pre exist
  4. course is not null or must pre exist
  5. assessmentManager is not null

These are just guesses from your code so I m sure you have more than these.

Now you want to write test that test each of these one at a time. Generally you want a test method to test precisely one thing. Some further information is also missing from the description but let us guess again.

In your struts2 action class you are setting a cancel object, an assessmentType, course and asessmentManager before this action is called. You then need to set all of these in your test method before you call the action.

So, to test the first point you want to set the variable cancel. Maybe like this:

 action.setCancel(Some Object);
 action.save();
 //now test the result condition to see if it behaved as expected.

Then to test the second point you can use code like this:

 //testing that null assessment throws NullPointerException
 action.setAssessment(null);
 action.save();
 //now test that the exception was thrown 

Then test that the non null assessment works...and so on. Each of these tests are a method by themselves.

I hope this helps.





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签