Enterprise Java

Using JaCoCo to measure system test coverage

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 \

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!

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Using JaCoCo to measure system test coverage

Opinions expressed by Java Code Geeks contributors are their own.

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Back to top button