Measure Code Coverage Using JaCoCo!
Let’s dive to analyze the Code Coverage by using a simple Open Source tool-JaCoCo.
It is always essential to have a bunch of unit tests to make your code free of bugs or issues and an organized code. But, are you aware that how many tests are required in an application? In other words, do you know whether unit tests cover all the lines of your code or all the branches of an “if” condition in a Java application?
Here, is the solution for it. 🏃🏻You can find the percentage of the code coverage using a simple Open Source tool -JaCoCo.
Repository for this article can be found here.
We will be covering the following in this article:
- Configuring jacoco-maven-plugin in pom.xml
- Coverage Threshold
- Code Without JaCoCo
- Code with JaCoCo
- Merits and De-Merits
Configuring jacoco-maven-plugin in pom.xml:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
By default, code coverage report will be placed in target/site/jacoco/index.html. In-order to change the location, you can include the following:
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<!--configure a different path-->
<configuration>
<outputDirectory>target/jacoco-report</outputDirectory>
</configuration>
</execution>
The report goal creates code coverage reports for tests in HTML, XML, CSV formats and the report is stored in the default path.
Coverage Threshold
Coverage Threshold is required to validate whether a set of coverage rules are met.You can add the below for setting a limit.
<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.9</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
According to the above snippet, a minimum value of 0.9 is required for the code coverage.
Having discussed about JaCoCo, let us try to understand how does an application work with and without configuration of JaCoCo.
Without JaCoCo:
Try running the application after cloning it by executing mvn clean test and you will observe the following:
What is happening here? We can see that all the tests are executed successfully but we aren’t sure about the code coverage which is required for a clean code.
That is where JaCoCo comes into the picture📸.
With JaCoCo:
Post running the application by executing mvn clean test, a report will be generated under /site/jacoco/index.html. Try opening the file in any of the browser like below.
Amazing !. It is such a feast to look into the above report . Isn’t it? As you can observe from the above images, it is understood that the coverage is 100%☑️.
If we have a look at the reports generated, we can see some color and shape in the report. The JaCoCo report helps us to analyze the code coverage by using 3 different colors:
- The red diamond indicates that no branch has been executed during the execution of the code.
- The yellow diamond shows that the code is partially covered; some branches have not been executed.
- The green diamond represents that all branches have been executed during the execution of the code.
The below metrics can be identified with the help of JaCoCo report:
- Lines Coverage- It denotes how many lines of code are executed
- Branch Coverage- It denotes how many branches of code are executed
- Cyclomatic Coverage-Reflects the complexity of the code by providing the number of paths required to cover all possible paths to the code by linear combination.
With JaCoCo Code Coverage Threshold:
Finally, when you had included code coverage threshold, the results can be viewed by the message “ All coverage checks have been met” or some failure exception occurs in the console after executing mvn clean verify as below:
Merits and De-Merits:
Merits:
- Very simple and easy to integrate being an Open Source tool
- Detailed Coverage Report on basis of methods,lines and branches in a code.
- Product is well maintained and multiple releases in a year.
- Very active community and scope of enhancement.
De-Merits:
The only disadvantage is JaCoCo is available only for Java applications.😐
That is all !. Hope you enjoyed my article. Give a clap and post your comments for any queries or recommendations!
You can also check YouTube for a visual view of this article.