Show Code Coverage in SonarQube for Maven Projects

Why is Code Coverage important for us?

Code coverage is an important metric for the application, which shows how much portion of your code was executed, or how many lines of code are yet to be covered with your tests. Higher code coverage implies that a large portion of your code is executed by your tests and successful execution of tests means the code covered in the code coverage report is working fine. So higher code coverage can be interpreted as more reliable code. In some tech giants, minimum 80% coverage rule is being followed, which means the build won’t go to production if code coverage for that build is less than 80%. Code coverage provides a detailed view on what part of your application is tested and what all branches got missed in your tests which helps to plan new tests to write.

Working with jacoco-maven-plugin

If you are using Jacoco for a code coverage report for a maven project you need to set up jacoco-maven-plugin in your pom.xml file. To configure jacoco-maven-plugin, you need to add these lines in the <plugins> section of your pom.xml:

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
</plugin>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco-ut.exec</destFile>
</configuration>
</execution>
  • report: This goal is important to generate the code coverage reports in multiple formats (HTML, XML and CSV) from execution info. In this goal you can configure classes to exclude from the report or classes to include and output directory and encoding of the reports. Make sure <dataFile> configuration is pointing to execution info file generated in prepare-agent goal. Here is the minimal configuration required for JUnit test code coverage:
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
<outputEncoding>UTF-8</outputEncoding>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
  • check: This is an important goal to automate your builds by applying constraints based on the code coverage. With the check goal, you can configure rules for your project to meet on code coverage, for example more than 80% code coverage, otherwise your build will fail. Here is the configuration for the same:
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>

Integrate Code Coverage with SonarQube

Once you have the coverage report generated, we can easily integrate it with SonarQube. To add code coverage in SonarQube, you just need to define a few properties for sonar-maven-plugin. To add sonar-maven-plugin for your project add these lines in <plugins> section of your pom.xml file:

<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</plugin>
<properties>
<java.version>1.8</java.version>
<sonar.language>java</sonar.language>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.coverage.jacoco.xmlReportPaths>${project.build.directory}/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
<sonar.jacoco.reportsPaths>${project.build.directory}/jacoco-ut.exec</sonar.jacoco.reportsPaths>
<sonar.tests>src/test/java</sonar.tests>
<sonar.projectKey>sonar-test</sonar.projectKey>
<sonar.host.url>http://localhost:9000</sonar.host.url>
</properties>
mvn clean install
mvn sonar:sonar -Dsonar.login=<your-token>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store