Search code examples
testingjunitcode-coverageemma

Junit: best way to test return statement


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.


Solution

  • 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.