sebastiandaschner blog


Using JaCoCo to measure system test coverage

#testing #quarkus monday, november 01, 2021

Measuring test coverage of your production code can quickly become a heated topic, especially with regards to which percentage is the right number. In the video, I want to show you a different approach to usual code coverage, one that uses system tests with the application’s endpoints, in order to see how well our test scenarios cover all use cases.

We will use JaCoCo and its JVM agent to gather the data while our application-under-test is running.

 

 

You can use the coffee-shop example on GitHub.

At first, we will build and run the applications as usual, with the difference that we attach the JaCoCo JVM agent to the application-under-test:

java \
  -javaagent:/.../org.jacoco.agent-0.8.7-runtime.jar \
  -jar target/quarkus-app/quarkus-run.jar

 

With this, we can use the application as usual and point the system tests towards it. The agent will place a jacoco.exec file in the working directory, which we can examine after the application has stopped.

The JaCoCo Maven plugin helps us to create a report. As shown in the video, we don’t have to add the plugin to our pom.xml, we can simply reference it in a fully-qualified way.

mvn \
  org.jacoco:jacoco-maven-plugin:0.8.7:report \
  -Djacoco.dataFile=jacoco.exec

 

This will generate a site under target/site/jacoco/, which we examine to see the actual coverage results.

As shown in the video, this approach differs a bit from the official Quarkus guide on test coverage for reasons mentioned. From my experience, measuring the coverage in this way is less intrusive to your project, more meaningful, and actually independent of the technology being used.

Happy code coverage measuring!

 

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java: