I use the Cobertura Maven plugin for one of my project. But I have a question about the generated report:
What is the difference between line and branch coverage?
Line coverage measures how many statements you took (a statement is usually a line of code, not including comments, conditionals, etc). Branch coverages checks if you took the true and false branch for each conditional (if, while, for). You'll have twice as many branches as conditionals.
Why do you care? Consider the example:
public int getNameLength(boolean isCoolUser) {
User user = null;
if (isCoolUser) {
user = new John();
}
return user.getName().length();
}
If you call this method with isCoolUser
set to true
, you get 100% statement coverage. Sounds good? NOPE, there's going to be a null pointer if you call with false
. However, you have 50% branch coverage in the first case, so you can see there is something missing in your testing (and often, in your code).