Code Coverage with JaCoCo and SonarQube
Introduction
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 important metrics. Even though SonarQube comes with lots of features out of the box, we will create the code coverage reports using the the JaCoCo library.
In this post we will create a very simple Maven project and configure SonarQube and JaCoCo to demonstrate how to generate code coverage reports.
Installing and Starting SonarQbe
The first step is download SonarQube from www.sonarqube.com. It works out of the box, no installation is needed. Just start bin/windows-x86-64/StartSonar.bat or bin/linux-x86-64/sonar.sh depending on your operating system and you're done. The Sonar server is up and running.
The Sample Project
We will look at the the code coverage function using a very simple project.
First, generate the project using the maven-archetype-quickstart archetype:
mvn archetype:generate -DgroupId=com.codingboost.example -DartifactId=example-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
Create a file called Adder.java and place it in the src/main/java/com/codingboost/example directory as follows:
package com.codingboost.example;
public class Adder {
public int add(int a, int b) {
return a + b;
}
}
Now create the corresponding test file called AdderTest.javain the foldersrc/test/java/com/codingboost/example.
The file itself will look like as follows:
package com.codingboost.example;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AdderTest {
@Test
public void testAddition() {
Adder adder = new Adder();
assertEquals(adder.add(2,3), 5);
}
}
With that being said, the final directory structure should look like this:
$ tree
.
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── codingboost
│ └── example
│ └── Adder.java
└── test
└── java
└── com
└── codingboost
└── example
└── AdderTest.java
11 directories, 3 files
$
Setting up JaCoCo
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:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Generating the Code Coverage Reports
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] BUILD SUCCESS
[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:

As we can see, the test is fully covered. Great!
Of course, JaCoCo can be used without SonarQube as well — just type the following to generate the reports:
mvn clean jacoco:prepare-agent install jacoco:report
After executing this command, the reports will be available in the target/site/jacoco folder:

Wrapping Up
We've seen how to install and start SonarQube, then also created a sample project we used in this tutorial. Then we looked at how to configure JaCoCo and trigger the code coverage report generation in two different ways.
Hope it was useful!