Random UTF-8 characters


22 Jan 2016 in coveragegradlejavacoveralls.io

Java: Code coverage with Gradle and JaCoCo

There are so many great metrics to help you improve a project's quality. One of these metrics is code coverage.

In this post we will go over why this measurement is important, how to measure coverage using JaCoCo. I'll also wrap up with some info on coveralls.io which allows you to mesure this metric between builds.

What is code coverage?

Code coverage is a measure of how much for your application's code has been executed in testing. This covers not only seeing which lines of code have been executed, but also checking that all branches have been covered.

The a coverage tool will ensure your tests sent at least 3 cases:

a = true
b = true
a = false, b = false
Why measure code coverage?

The more code that you have covered the lower the probability of potential bugs. Coverage is not a cure all for bug free code, but does force you to think about writing more tests.

After adding coverage reports to several work projects, I can tell you all of a sudden every developer on the team seems to commit a few more tests. No one wants to be the one that commits a large amount of untested code.

Setting up JaCoCo

I'll add coverage to a simple spring-boot web application. I have a pre-made simple boot app on git hub: https://github.com/brookslyrette/spring-boot-demo-starter. You can use this as a starting point. First download the code or clone the git repo git clone https://github.com/brookslyrette/spring-boot-demo-starter.git

You do not have to to add much to build.gradle to generate a JaCoCo report.

All you need to do to run your tests and generate a coverage report is gradle test jacocoTestReport

You can see I don't have proper coverage! I better add some more tests. Here is a small test for the DemoController

Now if we run our coverage results again, we get a nice improvement:

You can see the final product here in GitHub: https://github.com/brookslyrette/gradle-jacoco-coverage


While having coverage reports for the current code is great, it's even more useful to track changes in coverage over time. I stumbled onto a great tool for this http://coveralls.io.

It's free for any open source projects on GitHub or BitBucket and reasonably priced for private projects.

It keeps track of how code coverage changes over each build.

Configuring this is as simple as adding this plugin to your build: https://github.com/kt3k/coveralls-gradle-plugin