Code Coverage with Jacoco, Sonarqube, and Maven

Samuel Addico
CodeOps
Published in
3 min readMay 22, 2019

--

In my earlier post on What is BDD and Why, I explained what BDD is and why its necessary to adopt this development style. I also touched on the difference and relation between BDD and TDD. In this post, I will be demonstrating how to measure code coverage of your application using Jacoco and Sonarqube.

Note you will have to setup your own sonarqube server. You can follow this link to do that.Fortunately, you can also opt to use their online server (SonarCloud).

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. Code coverage is also called Test coverage. This gives developer teams reassurance that their programs have been broadly tested for bugs and should be relatively error-free.

Dependencies

To get this up and working we need to add the jacoco maven plugin as shown below.

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>CodeOps
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>…

--

--

Samuel Addico
CodeOps

DevOps | SRE | Java | Kubernetes | AWS | Ansible | Terraform | CI/CD