DEV Community

Cover image for Microservices code coverage with Jacoco
Bhargav M
Bhargav M

Posted on

Microservices code coverage with Jacoco

Frequent deployments is a huge step in Microservices architecture. If you have a suite of tests that run before every deployment and gives a green check mark to deploy with confidence. Should I trust the automated tests or not? one way to decide is by checking the code coverage. Jacoco helps to get code coverage details.

Jacoco is a java library that you can use it in your application to get the code coverage details. Jacoco agent gets installed on your jvm and that agent listen to jmx metrics and spits the coverage details. The output can be a file or socket. We can configure the path to the file and when jvm starts it locks the file and and when jvm stops it write the execution data to the file jacoco.exec. We can feed that file to static code analysis like Sonar and it will display the results or use jacoco reports plugin to generate reports. https://www.jacoco.org/jacoco/trunk/doc/agent.html

The above diagram shows the followings steps

1) Jenkins is running on a virtual machine which has its own jvm JVM-A which builds the application and deploys to the container on a remote host.

2) Once the app is running in a container on a virtual machine with jvm JVM-B where jacoco agent needs to be installed and configured. Jacoco agent on JVM-B spits out the execution data to a file (jacoco.exec) as configured.

3) Literally step 3 can be anything that understands the file generated in step2 and generates the reports. Sonar does that if we provide the path to the file. Maven has a plugin jacoco:report which understands jacoco.exec file and generates some nice ui reports.

How to do it

I have an example




which is simple rest service returns today's date with endpoint http://localhost:8080/today and run the following command

Generate the execution data file

java -javaagent:C:\Bhargav\jacocoagent.jar=append=true,includes=*,output=file,destfile=C:\Workspace\Github\Spring-Boot-Jacoco-Example\target\jacoco.exec -jar Spring-Boot-Jacoco-Example-0.0.1-SNAPSHOT.jar

javaagent:C:\Bhargav\jacocoagent.jar: pointing to the location where jacocoagent jar is present.

append : telling jacocoagent whether to append new metrics with old metrics if there is any old metrics

include : * means includes all classes, we can limit to a particular package

output : telling jacocoagent where you want to write the execution data, in this case we are telling jacocoagent to write it to a file.

destfile: location of the file where jacocoagent writes the execution data

The following file will get generated in the given destfile path of the above command.

Add descriptionexecution data generated by jacoco agent

Generate reports from the execution data file

Inorder make this file readable we can use a maven plugin called jacoco-maven-plugin


<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${basedir}/target/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

running it from the cmd

mvn org.jacoco:jacoco-maven-plugin:0.8.3:report

Add descriptionindex file<br>

Add descriptionactual class with code coverage details

Latest comments (0)