How to Set Up Code Coverage for a Java Project using Gradle, Travis, JaCoCo and Codecov

by Paul Vorbach,
2015-07-23

The code coverage of a project's test suite can be a useful measure for finding out about the quality of the project. There are several tools for Java that can calculate the code coverage, for example SonarQube and JaCoCo.

During my recent work on property-providers, I found out how to give users a quick overview of the test coverage of the code using Codecov. It is not well documented for projects that don't use Maven, so here I present the complete setup that brings the code coverage badge to the GitHub page of the project.

Gradle

The project uses Gradle instead of Maven. Here's its build.gradle. It configures JaCoCo reports. XML reports are disabled by default but those are needed in order to work with Codecov, so enable them. Also the check task, which is run by Travis, needs to depend on jacocoTestReport.

Codecov

In order to use Codecov, you need to sign up first. It is straight forward for Github users: just sign up with GitHub and then add the repository that you wish to monitor. The service, like Travis, is free for open source projects.

Readme

Finally, I added this markdown snippet to my README.md file in order to show the coverage badge. I used the badge from shields.io instead of Codecov's own badge, because it gives better consistency with other badges.