JaCoCo (Java Code Coverage) is a popular open-source toolkit for measuring and reporting code coverage in Java projects. When integrated with Spring Boot, it helps ensure your tests are effectively covering your application logic, improving reliability and maintainability.
What is JaCoCo?
JaCoCo is a free Java code coverage library distributed under the Eclipse Public License. It works with JUnit, TestNG, and other test frameworks, providing:
- Line coverage – How many lines of code were executed.
- Branch coverage – How many decision branches were executed.
- Instruction coverage – Low-level bytecode execution detail.
Why Use JaCoCo with Spring Boot?
- Measure test effectiveness – Ensure your unit and integration tests are covering critical logic.
- Improve code quality – Detect untested and potentially faulty code.
- CI/CD integration – Fail builds based on coverage thresholds.
- Track regressions – Ensure code coverage doesn’t drop over time.
Step-by-Step Integration of JaCoCo in Spring Boot
1. Add JaCoCo Plugin to pom.xml
(Maven Projects)
<build>
<plugins>
<!-- JaCoCo plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version> <!-- Check for latest version -->
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2. Run Tests and Generate Coverage Report
Execute:
mvn clean test
To generate the report:
mvn jacoco:report
3. View the Report
Open the report in your browser:
target/site/jacoco/index.html
You’ll see:
- Coverage by class
- Missed instructions
- Branch and line coverage visualization
Example Directory Structure
src/
├── main/
│ └── java/
│ └── com.example.demo/
│ └── MyService.java
└── test/
└── java/
└── com.example.demo/
└── MyServiceTest.java
JaCoCo will detect and include all tests from src/test
.
Custom Configuration Options
Set Coverage Thresholds
Fail the build if the coverage is below the set threshold:
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
Exclude Classes from Coverage
To ignore generated or config classes:
<configuration>
<excludes>
<exclude>**/config/**</exclude>
<exclude>**/dto/**</exclude>
</excludes>
</configuration>
Integration with CI/CD (GitHub Actions Example)
- name: Run tests and generate JaCoCo report
run: mvn clean verify
You can use plugins like codecov or coveralls to upload the report:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
files: target/site/jacoco/jacoco.xml
Integration with SonarQube
To send JaCoCo coverage data to SonarQube:
<properties>
<sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>
Then run:
mvn clean verify sonar:sonar
JaCoCo vs Other Tools
Feature | JaCoCo | Cobertura | Clover |
---|---|---|---|
Active development | ✅ Yes | ❌ No | ❌ No |
Integration with IDEs | ✅ | ✅ | ✅ |
CI/CD friendly | ✅ | ⚠️ (limited) | ❌ |
Branch coverage | ✅ | ❌ | ✅ |
Best Practices
- ✅ Aim for meaningful tests, not just high coverage.
- 🧪 Cover edge cases and failure paths.
- 📊 Set and enforce coverage baselines in CI.
- ⚙️ Exclude generated and config classes to avoid skewed reports.
Conclusion
JaCoCo + Spring Boot is a powerful combo to ensure your code is well-tested, maintainable, and production-ready. It integrates smoothly with Maven, CI/CD tools, and quality dashboards like SonarQube.
By automating test coverage tracking, you reduce bugs and improve code health—essential for modern, agile Java development.