DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice

Trending

  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • My LLM Journey as a Software Engineer Exploring a New Domain
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. JaCoCo End-to-End Code Coverage at Runtime

JaCoCo End-to-End Code Coverage at Runtime

Maintain code coverage from start to finish with this open source tool and a sample Spring Boot application to play with.

By 
Jagdish Raika user avatar
Jagdish Raika
·
Updated Sep. 10, 19 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
36.3K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

You want your code to be covered better than them

This article demonstrates how to generate coverage for integration/runtime tests using the JaCoCo agent that is based solely on runtime/integration tests. There are many articles on the Internet that explain code coverage using the Jacoco Maven plugin. The best part of this article is no use of any Maven plugin or unit tests (jUnit tests) to produce coverage reports.

JaCoCo is a free code coverage library for Java created by EclEmma.

You may also enjoy:  Verifying End-to-End Test Code Coverage Using Jacoco Agent

Before we move on to look at the code coverage capabilities of JaCoCo, we should have a look at the code sample. I have developed the simplest application based on the Spring Boot framework. The code for this application can be found at Github account.

@RestController
public class SampleController {

@GetMapping(path = "/test/{param}")
public String codeCoverageMethod(@PathVariable int param) {

if (param > 10) {
return callingThisMethod(param);
}
else {
return callingAnotherMethod(param);
}
}

private String callingThisMethod(int param) {

System.out.println("Calling this method.");
if (param < 10) {
System.out.println("This code cannot be called..");
}
return "Calling this method.";
}

private String callingAnotherMethod(int param) {

System.out.println("Calling another method.");
return "Calling another method.";
}

}


Report Generation

1. We should pass javaagent as a VM argument while running the application.

  • Command-line:
java -javaagent:<path-to-application>/src/main/resources/lib/jacocoagent.jar=address=*,port=36320,destfile=jacoco-it.exec,output=tcpserver -jar <path-to-application>/target/jacoco-code-coverage-example-0.0.1-SNAPSHOT.jar
  • For STS, we can configure it under Argument section and put below VM argument:
-javaagent:<path-to-application>/src/main/resources/lib/jacocoagent.jar=address=*,port=36320,destfile=jacoco-it.exec,output=tcpserver


To run it in the STS, right-click on the project and run it as a Spring Boot application and add all configurations. We can refer to the pictures below for this.

Run configuration

Run configuration

We have given the port and destfile as input to javaagent. Javaagent will dump all data (generated through the report) into the destfile and it is located on the given port of the server.

2. Now we will hit rest endpoints to check the code coverage. We can do this using browser/postman or curl command as shown below in the shell script.

curl -X GET http://localhost:8080/test/9
curl -X GET http://localhost:8080/test/50


3. Therefore, we have hit the rest of the endpoints. Now we are about to dump the file, which will be in exec format. This code will be all data collected from javaagent to analyze the code coverage. We will use this command to dump the report and the dump file will be inside the target folder.

java -jar <path-to-application>/src/main/resources/lib/jacococli.jar dump --address localhost --port 36320 --destfile <path-to-application>/target/jacoco-it.exec


4. We have collected the data, but it is not in a readable format. We can use the command to generate code coverage reports readable in many formats, including HTML, CSV, and XML. I am converting to HTML format here, so we will use the below command to create the final report.

java -jar <path-to-application>/src/main/resources/lib/jacococli.jar report <path-to-application>/target/jacoco-it.exec --classfiles <path-to-application>/target/classes/com --sourcefiles <path-to-application>/src/main/java/ --html <path-to-application>/target/jacoco-report 


To run all the above steps, I have created a shell script file that will automate all the steps:

mvn clean install
sleep 5
java -javaagent:src/main/resources/lib/jacocoagent.jar=address=*,port=36320,destfile=jacoco-it.exec,output=tcpserver -jar target/jacoco-code-coverage-example-0.0.1-SNAPSHOT.jar
sleep 5
curl -X GET http://localhost:8080/test/9
curl -X GET http://localhost:8080/test/50
sleep 5
java -jar src/main/resources/lib/jacococli.jar dump --address localhost --port 36320 --destfile target/jacoco-it.exec
sleep 5
java -jar src/main/resources/lib/jacococli.jar report target/jacoco-it.exec --classfiles target/classes/com --sourcefiles src/main/java/ --html target/jacoco-report

Note: To run the above script, the terminal must indicate the current location as the location of the project directory.

The code coverage report is ready. Now we can take a look at the target/jacoco-reports/index.html page to see what the generated report looks like.

Report Analysis

As we can see in the report, it shows 93% instructions coverage, 75% branches coverage.

Code coverage-SampleController.java

Sample controller

Code coverage-SampleController

Code coverage report

If we look at the reports generated, we can see some color and shape in the report. The JaCoCo report helps us analyze code coverage by using diamonds with colors for branches and background colors for lines:

  • The red diamond indicates that no branch has been executed during the execution of the code.

  • The yellow diamond shows that the code is partially covered; some branches have not been executed.

  • The green diamond represents that all branches have been executed during the execution of the code.

We can refer to the same color code for the background color, but this is for the coverage of the lines.

Source Code

The source code for this post can be found on Github account.

Code coverage

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!