Home » Java » Enterprise Java » Using JaCoCo to measure system test coverage

About 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.

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 \
  -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!

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments