Jacoco Source Code Coverage tool



Jacoco is the software metric used to provide how many lines of code covered in each source java file after executing the unit tests.

In this article, we are going to learn how to automate the Jacoco tool to invoke using the Maven build tool to provide the coverage report.

JaCoCo Maven Configuration:

Jacoco is providing the Maven plugin to get started and create the source code coverage report.

Add the below maven plugin configuration in the pom.xml file.

<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.5</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>target/jacoco.exec</dataFile> <outputDirectory>target/jacoco-ut</outputDirectory> </configuration> </execution> </executions> <configuration> <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> </configuration> </plugin>

Running the tests using Junit, will automatically detect the Jacoco agent, it will create the Coverage report in the data file specified in the plugin configuration.(target/jaoco.exec). This file will be in a binary file, and it is not human readable. But we can upload the exec file like some tools like SonarQube etc. Which can read the exec file, and provide the report in a human-readable format?

Jacoco Maven plugin has the goal report to generate the human-readable format like HTML file, below is the maven command to generate the report.

mvn jacoco:report

This maven goal will create index.html file outputDirectory specified in Jacoco plugin configuration if not target/site/jacoco/index.html page will be created by the plugin.

Jacoco also provides the Eclipse plugin EclEmma plugin, using which we can create the code coverage tool using defaults.

Report Analysis:

As Jacoco used byte code instructions to generate the report, it will check for all cyclomatic complexity and so on.

Jacoco report will have 3 different colors for different purposes.

1) Red diamond:

It means that the lines code/no branches of the conditions not covered in unit tests.

2) Yellow diamond:

It means some branches of the code are covered, and some branches of the code not covered during the tests.

3) Green diamond:

It means all lines of the code/all branches of the code covered during the tests.

Jacoco provides mainly three important metrics:

1) Line Coverage:

It reflects how many lines of code are executed based on java byte code instructions called from test cases.

2) Branch Coverage:

It shows how many branches of the statement in code, called from test cases, like if/else and switch statements.

3) Cyclomatic complexity:

Reflects the complexity of the code by providing the number of paths required to cover all possible paths to the code by linear combination.

Jenkins: A Continuous Integration Tool

As based on Maven, it can also run in continuous integration tools like Jenkins. After a developer commits the code, we can execute the test with code coverage, and we can put some threshold value for minimum coverage percentage so that without writing unit test Jenkins will not allow committing the code, which can improve the code quality the project.

Execution inside Jacoco plugin

Add the below execution inside the Jacoco plugin configuration:

<execution> <id>jacoco-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>PACKAGE</element> <limits> <limit> <counter>LINE</counter> <value>COVEREDRATIO</value> <minimum>0.90</minimum> </limit> </limits> </rule> </rules> </configuration> </execution>

If the Code coverage is less than 90%, then build fail, in turn, the Jenkins job failed.

Jacoco also provides another goal to simply check code coverage percentage without generating the report.

Worried about the number of lines covered in the unit tests executed source java files?

Get a clear picture of the facility provided by the JaCoCo source code coverage tool by visiting us and get a complete report.

mvn jacoco:check is the maven command to verify the coverage percentage.

Configuring the plugin using Maven surefire plugin:

Maven surefire plugin used to execute the tests, Jacoco provides the JVM agent is responsible for invoking the Jacoco Analysis tool.

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.15</version> <configuration> <argLine>${surefireArgLine}</argLine> </configuration> </plugin>

During the execution time, surefireArgLine property will be resolved and add the Jacoco JVM agent, and it will trigger the Code coverage analysis tool.


A Leading Java Application Development Company here shows you what is Jacoco and How to use this tool by integrating the Maven build tool, different color Conventions used in the Jacoco report. We learned the different Maven goals used to trigger the Jacoco maven plugin, and finally, we learned how to integrate with the maven surefire plugin to trigger the analysis.

Related article

Maven is one of the most used tools for Java application development. Maven packaging will create a Jar file without dependencies.

Spring Cloud Config provides server and client modules for supporting externalized Configuration in a distributed system.

The Internet of Things abbreviated as IoT is the network of devices that can be connected to the internet and other devices.

DMCA Logo do not copy