Junit: best way to test return statement

2.4k views Asked by At

I am using Junit 4 and I have run into a little problem that I don't really know how to fix.

I have the following statement:

public boolean foo(int someId) {
   //bla bla
   return updatedLines != 0; //Returns true or false
}

or this:

public boolean foo(int someId){
   //bla bla
   return (someField.equalsIgnoreCase("value") && someValue > 0);
}

Now my question is: how can I test my return results properly? Now I just check what my method returns (true or false) but my return line isn't covered according to Emma.

2

There are 2 answers

0
Tom Jonckheere On BEST ANSWER

You have to make sure that all possible outcomes are tested. So for the first statement you have at least two testcases: one where true is returned, the other where false is returned. If there are more branches is your return statement, you need to make sure that all are tested.

0
dkatzel On

Make sure that you are testing every possible combination?