How to setup JaCoCo code coverage with Maven & Gradle

Step by step configuration of the JaCoCo code coverage tool for Java project using Maven & Gradle build tools

Image for post
Image for post
Source: https://dilbert.com/strip/2011-03-24

Java code coverage tools

There are different code coverage tools for different languages, and I am going to focus on Java. The most famous code coverages tools are

  1. Cobertura — https://cobertura.github.io/cobertura/
  2. Open Clover — https://openclover.org/
  3. JCov — https://wiki.openjdk.java.net/display/CodeTools/jcov

What is JaCoCo?

JaCoCo is an open-source code coverage tool that measures coverage of

  • Lines
  • Branches
Image for post
Image for post
JaCoCo code coverage report

How to setup JaCoCo with Maven?

Here is what you need to setup JaCoCo in a Maven-based project. Let’s generate a maven-based spring boot project and then set it up with the JaCoCo code coverage tool.

Step 1. Generate the project

Navigate to https://start.spring.io/ to generate a project with an appropriate version of build tool (maven), language (Java), the default version of spring

Image for post
Image for post
Generate a Maven-based Java Spring Boot project

Step 2. Import project into IDE (i.e., Eclipse)

The above would generate a zip demo.zip file, and you can extract it in your workspace. Let’s get started and I will use Eclipse IDE

cd demo 
mvn eclipse:clean
mvn eclipse:eclipse
Image for post
Image for post
Import an existing maven project into Eclipse
Image for post
Image for post
Import project into Eclipse

Step 3. Add JaCoCo plugin to the pom.xml

Now that the project is added to your IDE, let’s modify the pom.xml to add the JaCoCo configuration.

Step 4. Generate code coverage report

In order to generate a code coverage report, run the mvn test in the project and you will see the following lines relating to jacoco towards the end of the console

...
[INFO] --- jacoco-maven-plugin:0.8.5:report (report) @ demo ---
[INFO] Loading execution data file /Users/raf/workspace/demo/target/jacoco.exec
...
Image for post
Image for post

How to setup JaCoCo with Gradle?

Gradle is another alternative to the Maven build tool. Let’s configure JaCoCo in a Java project that uses Gradle.

Step 1. Generate the project

Navigate to https://start.spring.io/ to generate a project with an appropriate version of build tool (Gradle), language (Java), the default version of spring

Image for post
Image for post
Generate a Gradle-based Java Spring Boot project

Step 2. Import project into IDE (i.e., Eclipse)

Let’s update the project’s build.gradle file and under the plugins include eclipse as shown below

plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'eclipse'
}
Image for post
Image for post
Import a Gradle project into Eclipse

Step 3. Add the JaCoCo configs to build.gradle

Looking into https://docs.gradle.org/current/userguide/jacoco_plugin.html documentation, to get the JaCoCo setup going with Gradle we need to add the jacoco under the plugins in the build.gradle file as shown below

plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'eclipse'
id 'jacoco'
}
./gradlew jacocoTestReport 
./gradlew jacocoTestCoverageVerification
jacoco {
toolVersion = "0.8.5"
reportsDir = file("$buildDir/jacoco")
}
  1. When the jacocoTestReport is run, then first run the test and after the the jacocoTestReport
test {
finalizedBy jacocoTestReport
}
jacocoTestReport {
dependsOn test
}

Step 4. Generate code coverage report

Now that we have done the basic setup of JaCoCo with Gradle let’s run the coverage report and we can do that using either of the following commands

./gradlew test
./gradlew jacocoTestReport
Image for post
Image for post
Run Gradle Test which also runs JaCoCo report
Image for post
Image for post
The JaCoCo code coverage file
Image for post
Image for post
A Gradle-based code coverage report

JaCoCo Execution Goals

There are 11 execution goals with JaCoCo, and you can see the list by running mvn jacoco:help and some of them is listed below

  1. The command mvn jacoco:dump — Request a dump over TCP/IP from a JaCoCo agent running in TCP server mode.
  2. The command mvn jacoco:merge — Merges a set of execution data i.e., jacoco.exec into a single file.
  3. The command mvn jacoco:prepare-agent — Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test
  4. The command mvn jacoco:report — Create a code coverage report in multiple formats (HTML, XML, and CSV).

JaCoCo Rules to Enforce code coverage metrics

You can add rules to your JaCoCo configuration to enforce certain code coverage metrics.

  • PACKAGE
  • CLASS
  • SOURCEFILE
  • METHOD
  • LINE
  • BRANCH
  • COMPLEXITY
  • METHOD
  • CLASS
  • COVEREDCOUNT
  • MISSEDCOUNT
  • COVEREDRATIO
  • MISSEDRATIO
rule element: BUNDLE
limit counter: INSTRUCTION
limit value: COVEREDRATIO
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit>
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
<rules>
<rule>
<element>CLASS</element>
<excludes>
<exclude>*Test</exclude>
</excludes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>50%</minimum>
</limit>
</limits>
</rule>
</rules>
jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
limit {
minimum = 1
}
excludes = [
'package.ClassA',
'package.ClassB'
]
}
rule {
limit {
counter = 'INSTRUCTIONS'
value = 'MISSEDRATIO'
minimum = 0.3
}
}
}
}
  • Each rule can be applied to an element type i.e., BUNDLE, PACKAGE, CLASS, SOURCEFILE , and METHOD using the element field.
  • The limit takes a counter which can be either INSTRUCTION, LINE, BRANCH, COMPLEXITY, METHOD, or CLASS using the field counter
  • The limit takes a value which is either TOTALCOUNT, COVEREDCOUNT, MISSEDCOUNT, COVEREDRATIO, or MISSEDRATIO using the field value
  • The limit also takes a minimum or maximum

Conclusion

Thank you for reading this article. I have published an article on how to contribute to open-source if that’s a topic that interests you

A Human first • Senior software developer • Loves to write with over 200K views on Medium • Co-founder in the making • Let’s connect on linkedin.com/in/rhamedy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store