Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've recently had a chance to compare test coverage reports between JaCoCo version 0.7.8 and version 0.8.1 and I must say that I'm impressed. Since JaCoCo 0.8.0, new filtering options have been available and enabled that skip some code blocks that really do not need to be tested and do not need to be reported on in test coverage reports.

From the JaCoCo developer wiki pages, these are the filters that have been implemented...
JAVAC.SYNTHMETH - synthetic methods
JAVAC.SYNC - release lock code in synchronized statements
JAVAC.FINALLY - finally block emitted multiple times
JAVAC.ENUM - static methods and initializer generated for enum types
JAVAC.TRYWITH - extra exception handlers installed to close resources in try-with-resources statements
JAVAC.STRINGSWITCH - extra branches due to implementation with hashcode and equals comparison
empty constructor without parameters in enum - Done in 0.8.1
Private empty constructors that do not have arguments

So what does that mean practically? Well, I've taken a closer look at one particular instance within the code base I'm working on - one of the Enum classes. With the older version of JaCoCo the coverage looked like this...

The values() and valueOf() methods had zero coverage. These methods were not part of the source code that was written so of course they weren't tested. Switching to the new JaCoCo plugin changed the coverage report to exclude these synthetic methods...

An 11% improvement in code coverage! Not bad for not doing anything. Of course this didn't mean that the code was better tested, it simply meant that any methods that didn't need testing were not reported on.

In terms of the whole code base, code coverage went up by 3%. It's important to note that the number of total counted instructions and branches has reduced with the new version of JaCoCo - again this is because anything that doesn't need testing is not reported on. The code itself has not changed.

Here are some more detailed reports on the whole code base for one of the modules I was running this on. Before JaCoCo...

After JaCoCo...

This is a great improvement to JaCoCo simply because now we don't have to focus on writing those 'fake tests' to increase code coverage just because the reporting tool wasn't smart enough to filter out noise. Hopefully the rest of the filters are implemented soon and JaCoCo becomes even more robust.

If you use JaCoCo with PowerMock you may find this article useful - JaCoCo reports missing code coverage for tests using PowerMock.


Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...