Code Coverage with JaCoCo and SonarQube

Robert Torok August 15, 2018
Code Coverage with JaCoCo and SonarQube

SonarQube is a great tool for code quality inspection, it helps you detect issues and catch bugs early. In addition to quality inspection, it can also show code coverage and other metrics as well. In this post we'll create a very simple Maven project and configure SonarQube.

The first step is to download SonarQube from It works out of the box, no installation is needed; just start bin/windows-x86-64/StartSonar.bat or bin/linux-x86-64/ depending on your platform and you're done, the Sonar server is up and running.

We'll demonstrate the code coverage function on a very simple method as follows:

public class Adder {
    public int add(int a, int b) {
        return a + b;

And the corresponding test case:

public class TestAddition {
    public void testAddition() {
        Adder adder = new Adder();
        assertEquals(adder.add(2,3), 5);

The code coverage report will be generated by the JaCoCo plugin, it basically feeds SonarQube with coveraga data. To use JaCoCo, add the following snippet to your pom.xml in the <build> and <plugins> section:


After executing mvn clean install, the application complies, the tests run and the coveraga data is generated. Now you can execute mvn sonar:sonar to have SonarQube analyze our application. Once the analysis is completed, you should see something like that:

[INFO] Analysis reports compressed in 17ms, zip size=14 KB
[INFO] Analysis report uploaded in 18ms
[INFO] ANALYSIS SUCCESSFUL, you can browse http://localhost:9000/dashboard/index/net.roberttorok:samples.phonebook
[INFO] Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[INFO] More about the report processing at http://localhost:9000/api/ce/task?id=AVxwFbzSxHGXjOSq_T0l
[INFO] Task total time: 2.724 s
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.138 s
[INFO] Finished at: 2017-06-04T00:31:31+02:00
[INFO] Final Memory: 19M/571M
[INFO] ------------------------------------------------------------------------

Navigate to the given URL and you'll see the reports:

Test fully covered

As we can see, the test is now fully covered.