"A Comprehensive Overview of JaCoCo: Java Code Coverage Tool, Features, and Best Practices for Optimizing Test Coverage"
October 10, 2024

An overview of JaCoCo
A code coverage library that is widely utilized for Java projects is JaCoCo (Java Code Coverage). The tool assists developers in determining how much of their code is covered by unit tests, providing insights into areas that are not extensively tested. Both Maven and Gradle build systems are supported by JaCoCo, and it is commonly used together with Continuous Integration (CI) tools to monitor code coverage over time.
What is Code Coverage?
Code coverage is a metric used in software testing to determine which lines of code have been executed (covered) during automated tests (e.g., unit tests). It gives an indication of how much of the code is actually tested.
There are various types of coverage metrics that JaCoCo can measure:
- Line Coverage: The percentage of lines executed by tests.
- Branch Coverage: Measures whether all branches in conditional statements (e.g., if, switch) are covered.
- Method Coverage: The number of methods that have been executed.
- Class Coverage: The percentage of classes that have been covered by tests.
Key Features of JaCoCo:
- Integration with Popular Build Tools: JaCoCo easily integrates with build tools like Maven, Gradle, and Ant.
- Detailed Coverage Reports: JaCoCo generates HTML, XML, and CSV reports to provide detailed insights into code coverage.
- Continuous Integration Friendly: JaCoCo can be integrated into CI/CD pipelines (e.g., Jenkins, GitLab CI) to track code coverage over time and ensure quality.
- Supports Java 8 and Above: JaCoCo is compatible with modern Java versions and can instrument complex Java applications.
- Exclusions and Filtering: Developers can exclude specific packages, classes, or methods from coverage, especially when they are not relevant to testing (e.g., generated code, model classes).
Step 1: Add JaCoCo Maven Plugin
In your pom.xml, add the JaCoCo plugin in the <build> section:
<build>
<plugins>
<!– JaCoCo plugin –>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version> <!– Use the latest version –>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal> <!– Instrumentation for code coverage –>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal> <!– Generate the code coverage report –>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The code will be automatically instrumented during the test phase and a code coverage report will be generated after the tests are executed with this setup.
Step 2: Run Tests and Generate Report
To generate the code coverage report:
- Run your tests using Maven: mvn clean test
- Run the following to generate the JaCoCo report: mvn jacoco:report
Target/site/jacoco will produce the coverage report. Target/site/jacoco/index.html is where the HTML report will be found.


From the above image, it is apparent that maven-unit-test is the project name and com.swarck.examples is the package. According to the report, 78% of the code was covered and 83% of the branches were covered. To learn more about Jacoco properties, refer to this resource : Counters
The package contains three classes: Palindrome , Factorial and MagicBuilder. The focus should be on Palindrome, which covers 82% of the code and 75% of branches.

Go into the Palindrome class and access the isPalindrome(String) method. The outcome will be as follows.

JaCoCo generates coverage reports that look something like this:

- Green Lines: Fully covered code.
- Yellow Lines: Partially covered (e.g., one branch of a conditional is tested).
- Red Lines: Code that is not covered at all.
Step 3: Configure Code Coverage Thresholds (Optional)
To make sure that there is no overlap, you can enforce a minimum code coverage threshold by including the following configuration in the jacoco-maven-plugin:
<build>
<plugins>
<!– JaCoCo plugin –>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal> <!– Add the check goal –>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum> <!– 80% minimum coverage –>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Step 4: Exclude Package/Classes (Optional)
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<excludes>
<exclude>com/swarckinfolabs/package1/* </exclude> <!– Exclude all classes in package1 –>
<exclude>com/ swarckinfolabs /package2/MyClass* </exclude> <!– Exclude MyClass and its inner classes in package2 –>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Key Points:
- To exclude certain classes or packages, use wildcards (*).
- You have the option to exclude either entire packages or specific classes.
- The MyClass* pattern allows for the exclusion of inner classes.
Conclusion
JaCoCo is a tool that can be used to track code coverage in Java applications in a powerful and flexible manner. Integrating with build tools and CI/CD pipelines is easy, and it helps developers and teams ensure that their codebase is thoroughly tested. By utilizing JaCoCo, you can gain valuable insights into the testability and quality of your code, which will result in more robust and reliable software.
Get In Touch
"Share your ideas and get the best software service in the industry."
No comment