Java Jacoco. How To Check Java Test Coverage With Jacoco

Oleksii Dushenin
3 min readJul 30, 2021

In a How To Check Java Source Code With PMD post, PMD was introduced as an option to check the Java source code based on common programming flaws. The next step is to validate the build in accordance with Java Test Coverage. For this purpose, Java Jacoco can be used. In this post, we will see how to add Jacoco to the project. It will be responsible for Java Test Coverage.

Java Jacoco as a Java Test Coverage Tool

In this post, a Gradle project will be used as an example for Jacoco usage. The initial project can be created the same way as it was done for the How To Format Your Java Code With CheckStyle post.

Let’s create a class that has to be covered with tests.

package com.datamify.development.jacoco;public class Division {    public long divide(long dividend, long divisor) {
if (divisor == 0L) {
throw new IllegalArgumentException("Divisor cannot be 0");
}
return dividend / divisor;
}
}

Let’s create a test for this class. However, the exception condition will not be tested.

package com.datamify.development.jacoco;import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;class DivisionTest {    @Test
public void shouldDivide() {
assertEquals(3L, new Division().divide(21L, 7L));
}
}

The next step is the usage of Jacoco Gradle Plugin. It should be enabled in build.gradle.

plugins { 
id 'jacoco'
}

Let’s configure the plugin.

jacoco { 
toolVersion = "0.8.7"
}

A specified Jacoco version will be used.

test { 
finalizedBy jacocoTestReport
}

Each Gradle test task will be followed by report generation.

jacocoTestReport { 
dependsOn test
reports {
xml.required = true
csv.required = true
html.required = true
}
}

In this block, we specify that tests have to be executed before report generation. Also, report types are specified.

jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.8
}
}
rule {
limit {
counter = 'BRANCH'
value = 'COVEREDRATIO'
minimum = 0.8
}
}
}
}

In a previous code snipped, coverage verification options are specified. Minimum code coverage and branch coverage ratio of at least 80% are required. Rules and values depend on specific project needs.

Possible values can be found at Jacoco Coverage Verification Javadoc.

check.dependsOn jacocoTestCoverageVerification

The last step is required to fail a build when the coverage is lower than expected.

Let’s build the project.

gradlew clean build

The build fails with the next error:

FAILURE: Build failed with an exception.* What went wrong:
Execution failed for task ':jacocoTestCoverageVerification'.
> Rule violated for bundle development-jacoco: instructions covered ratio is 0.6, but expected minimum is 0.8
Rule violated for bundle development-jacoco: branches covered ratio is 0.5, but expected minimum is 0.8

The reports are generated in build/reports/jacoco directory.

Also, by clicking o the required packages and classes we can find the Division class coverage statistics.

As you can see, everything works as expected. We haven’t covered the exception branch on purpose and it has to be fixed.

Summary

In this post, Jacoco was used to validate the source code from the test coverage perspective. Based on project needs, you can select the appropriate rules with their limits. It can be the next step in CI/CD pipeline. The check will be done automatically. As a result, the review process will be slightly easier. The reviewer can focus on business logic and not on the test coverage.

The source code is available at Github.

Originally published at https://datamify.com on July 30, 2021.

--

--