Question

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 have found of making the test continue executing after the System.exit call.

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

I know this is an old question and that Cobertura has already fixed this, but for completeness the missing coverage on the "}" was caused by the internal automatically "finally" block.

See your code as this:

try {
    System.exit(0);
} catch (final SecurityException exception) {
    exception.printStackTrace();
} finally {
    // noop
}

Fortunately this is no longer happening for some versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top