views:

463

answers:

3

I am using the Maven (2) Cobertura plug-in to create reports on code coverage, and I have the following stub I am using in a method:

try {
    System.exit(0);
} catch (final SecurityException exception) {
    exception.printStackTrace();
}
System.err.println("The program never exited!");

I know that I need to log the exception, etc, but that's not the point right now...Cobertura is refusing to acknowledge that the line after the stack trace is printed is covered. That is, the line with the '}' before the System.err.println statement is not being shown as covered. Before, the ending curly brace of the method was not being shown as covered, hence the System.err statement. Any idea how I can convince cobertura's maven plugin that, since the System.err.println statement is covered, that ending brace has to have been covered?

Oh yeah, and I use a mock security manager to throw the security exception, since that's the easiest way I've found of making the test continue executing after the System.exit call.

+3  A: 

I would look at the coverage report. Double check my tests. Notice that the code really is getting covered and not worry about hitting 100%. Code coverage is best used to find areas that you may have neglected to hit with your tests, but just focusing on getting 100% coverage as a goal is bad habit that can lead to you skipping tests that need to be written just because your tool shows 100%. Use the tool for what it can do but don't fall into the trap of letting the tool define what you do.

tvanfosson
I agree with your statement, however it is very annoying not being able to reach "perfect" code coverage when the code above and below a brace is covered. These are tests that I'm making when the code is fresh, so there's not too much functionality yet.
MetroidFan2002
Perfect coverage is a fallacy. You need to spend your time covering the complex code and not the getters and setters. Coverage is valuable only to the extent where it helps you achieve the goal you are paid for - delivering a product, not reports.
ddimitrov
Perfect coverage is not the be-all end-all of metrics, certainly, but I disagree with your sentiment on testing getters and setters. Many is the time that a getter or setter has been modified to have a side-effect that is undocumented, and it is hard to track down. Killing a test solves this issue
MetroidFan2002
>> "Many is the time that a getter or setter has been modified to have a side-effect that is undocumented, and it is hard to track down" Do you have any empirical data? My personal experience with medium size systems (100-1000 kloc) tells me that's not the case ...or maybe I'm just lucky :-)
ddimitrov
You're just lucky :) One of the particularlily nasty side-effects I've found is a getter which performs a O(n) computation on its output before returning...if a test case was created to explicitly test that O(n) functionality, it would have been easy to track down and eliminate.
MetroidFan2002
+1  A: 

I haven't used Cobertura in a while (2005?), and saw this behavior back then. A similar problem exists with NCover for C# and curly braces following catch/finally blocks.

My suggestion would be to add to this Cobertura bug report detailing a similar issue. Also, follow @tvanfosson's advice and realize not having coverage on a curly brace, which doesn't actually become anything in the JVM, is something you can ignore as 'noise'.

sixlettervariables
Thanks, I added my code example to the bug you listed. Hopefully this bug will be fixed in the future, I didn't know it was a bug until you posted that link. Just thought it was some configuration issue I missed!
MetroidFan2002
+1  A: 

In the Java classfile format every method is annotated with a table mapping code offsets to line numbers. In this case, the closing brace does not produce any bytecode, hence it's not covered. This is an issue of imperfect correspondence between source and bytecode. It should be handled by the coverage tool, recognizing this line as non-code.

I know that Emma has similar issues. Clover fares much better, but is commercial (not sure if it would handle this case also). If you use IDEA, you should try their new coverage implementation - it's quite good and in active development.

ddimitrov
I'll pass on anything not free ;) As for my IDE, I use Eclipse quite extensively. I would try out Emma but unfortunately it doesn't have a Maven 2 plugin that I'm aware of, so for now Cobertura's the best option that I know of.
MetroidFan2002