Document toolboxDocument toolbox

Jacoco Code Coverage

Introduction

Jacoco is a Java code coverage tool which measures your code coverage, generates helpful HTML pages, can enforce minimum line and branch coverage in the build. It can be integrated with Jenkins so you can view all coverage metrics for your project.

Jacoco has been added to Synapse-Repository-Services, but it can be added to any Java Maven project.

Enabling JaCoCo in Jenkins

There are two ways to view the Jacoco reports in your jenkins build:

  1. Using the JaCoCo Jenkins Plugin

  2. Using the maven plugin itself

JaCoCo Jenkins Plugin

You can enable a post-build action for your build that generates a report integrated into Jenkins and that shows the coverage report within Jenkins as well as a graph with the coverage over time of your builds:

The plugin can be configured in your build adding a new post build action. Go to your project and click “Configure”, scroll down to the “Post-build Actions” section and add the “Record JaCoCo Coverage Report“:

The plugin needs to be configured with the path of the class directories pointing to “**/target/classes“:

When a new build is run, the coverage report and the coverage trend will appear in the build page:

JaCoCo Maven Plugin on Jenkins

To enable Jacoco in your devp* build in Jenkins, you’ll need to enable build artifacts in your project. Go to your project, click “Configure”, and scroll down to “Post-build Actions”. Add the “Archive the artifacts” action and set “Files to archive” to “**/*” (that is, everything). (Note the terminology. In this case “archive” means “save the artifacts so they’re available for me to look at after the build is done.)

This enables Jacoco for all your builds going forward.

To view your code coverage, go to one of your build instances, and click on “Build Artifacts”.

Jacoco is configured at the root of Synapse-Repository-Services, but executes on a per-project basis. To find your build artifacts, click through the folder directory to your project (example, “services”, then ”repository-managers”), then click “target”, the “site/jacoco”, then scroll down to “index.html”.

Running Jacoco in the command line

By default, running mvn install will run everything, including Jacoco coverage checks. To open your coverage report, browse to target/site/jacoco/index.html. (On a Mac, you could just run open target/site/jacoco/index.html.)

If for whatever reason, you want to generate Jacoco coverage reports, but don’t want to run everything associated with install, you can run mvn test jacoco:report jacoco:check. (Note that in order to generate coverage reports, Jacoco still needs to execute the tests.)