Jacoco

Overview:

JaCoCo (Java Code Coverage) is a free, open-source code coverage library for Java applications. It is widely used by developers and organizations to measure the effectiveness of their unit tests by providing insights into which parts of the code are being executed during testing. JaCoCo is highly customizable, integrates seamlessly with popular build tools and CI/CD pipelines, and supports Java 8 and later versions.

Key Features of JaCoCo:

1. Wide Tooling Support

  • Works with Maven, Gradle, Ant, and other build systems.
  • Integrates with CI tools like Jenkins, GitHub Actions, and GitLab CI.

2. Comprehensive Coverage Metrics

  • Provides line, branch, instruction, method, and class coverage metrics.
  • Helps developers identify uncovered or under-tested code.

3. Coverage Reports

  • Generates HTML, XML, and CSV reports for detailed analysis.
  • Highlights covered and uncovered code with visual indicators.

4. Java Agent

  • Uses a lightweight Java agent to collect coverage data at runtime.
  • Requires no modification of the application code.

5. Real-Time Monitoring

  • Enables on-the-fly monitoring of code coverage during application execution.

6. Support for Multi-Module Projects

  • Merges coverage reports across multiple modules, making it suitable for large projects.

7. Integration with Code Quality Tools

  • Seamlessly integrates with tools like SonarQube to enforce code quality standards and coverage thresholds.

Workflow of JaCoCo:

Setup:

  • Add JaCoCo as a dependency in the build tool (e.g., Maven or Gradle).

Give Maven application demo

1. Ensure JaCoCo Plugin is Configured in pom.xml file

add below configuration in demo application pom.xml file.

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version> <!-- Latest version -->
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

2. Run Maven Verify Command

Run the following Maven command to generate the JaCoCo report:

use command:

mvn clean verify

This will execute your tests and generate the coverage report.

3. Locate the Report

After running the above command, the JaCoCo report will be available in the following location:

target/site/jacoco/index.html

Open this index.html file in a browser to view the coverage report.

Mahesh Wabale
Latest posts by Mahesh Wabale (see all)

Leave a Comment