views:

195

answers:

1

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";

}
A: 

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.

Vincent Ramdhanie