Jacoco for unit test coverage with SonarQube tutorial

This tutorial extends SonarQube with Maven Tutorial – Code Quality for Java developers to use Jacoco for tracking unit test coverage. Jacoco is the default code coverage tool that gets shipped with SonarQube. SonarQube can also be configured to use Cobertura as the code coverage tool.

This assumes that Java 8 and Maven 3 are set up. The SonarQube is setup and running on port 9000.

Step 1: The pom.xml file needs to include the following two lines under properties tag.

The revised pom.xml with sonar & jacoco will look like:

Step 2: On the command-line execute the following mvn command

To get coverage per test, use the profile “coverage-per-test

Step 3: On the command-line execute the following mvn command

Step 4: Go to sonar web at http://localhost:9000, and click on the “coverage” tab to get the metrics on the code coverage as shown below.

Jacoco for unit test coverage with SonarQube

Jacoco for unit test coverage with SonarQube

You can drill into code level to see where the coverage needs to be enhanced.


300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials

Top