Spring Boot Coverage metrics with Jacoco and Sonar
Table of Contents
One of the main metrics for a software project is Test Coverage: if done properly, it gives you a quick picture of how much code is protected by tests.
In this article, I’ll show you how to use a minimal configuration to include JaCoCo in your Spring Boot application, and how to process this information with Sonar.
The JaCoCo library is one of most extended solutions for measuring coverage. If you want to explore some other options you can check out this comparison by Atlassian of code coverage tools (warning: may be biased, they sell one of the options, Clover). I’ve chosen JaCoCo since it’s free and really quick to set up if you know how (and you will after reading this guide :)).
Basically, you need to include some extra configuration in your pom.xml file and then, every time you execute the maven test phase, the JaCoCo plugin will generate the surefire reports.
To achieve that, you need to configure the JaCoCo plugin in your
pom.xml file (check maven repository for the latest stable version):
pom.xmlfile is part of a project available on GitHub: Code Quality Game. If you find it useful, please give it a star!
Running the plugin
With this simple configuration, you already have all you need to execute test coverage. Next time you execute maven with a goal that includes the
test phase, JaCoCo will generate the reports for you. For instance, if you execute:
You’ll see in the output a message like this:
As you can see a new file is being generated in the target directory. This file format is not really useful for a human but it can be sent to Sonar for further analysis, or processed by several different tools (including IDEs) to show detailed reports. In the next section, we see how to connect this jacoco.exec file with SonarQube.
Sending the report to Sonar
SonarQube has a really good integration with test code coverage. It allows you to analyze which parts of the code should be better covered, and you can correlate this with many other stats. If you want to send your report to your Sonar server the only thing you need to do is execute:
This command will try to reach Sonar at localhost if you don’t have an explicit configuration in your pom.xml file. If you want to connect with a different host you can also do it from the command line. For example, this command sends the JaCoCo output to a Sonar Docker image running on a VirtualBox:
This is how your command output will look like. The Sonar sensors will detect JaCoCo output and will process it:
That’s all. Now you can go to the Sonar Web interface and check your code coverage there. You will see not covered pieces of code with messages like the one shown in the screenshot below: ‘Not covered by Unit Tests‘.
docker-compose.ymlfile, so you can run SonarQube in a Docker container and see the coverage results. If you find it useful, please give it a star!
If you want to know more:
- Check my Complete Guide to REST (Controller) Tests in Spring Boot: Unit and Integration Tests
- Visit A Gamification experiment with Sonarqube if you’re interested in knowing more about the sample project
- Buy my book to get real experience with Spring Boot applications and proper Testing.