Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Master reporting code coverage using Maven and JaCoCo plugin with our step-by-step tutorial. Learn to generate precise code coverage reports effortlessly.
Harshit Paul
December 30, 2025
Code coverage is a software quality metric commonly used during the development process that lets you determine the degree of code that has been tested (or executed). To achieve optimal code coverage, it is essential that the test implementation (or test suites) tests a majority percent of the implemented code.
There are a number of code coverage tools for languages like Java, C#, JavaScript, etc. Using the best-suited code coverage tool is important to understand the percentage of code tested and take appropriate actions to ensure that you achieve the ideal code coverage!
For optimal code testing, many companies use the JaCoCo-Maven plugin that helps generate detailed code coverage reports. JaCoCo-Maven plugin is a free code coverage library for Java projects. It is based on the study of existing integration libraries that were created by the EclEmma team. At a larger extent, code coverage does give a brief overview of the product quality as higher the coverage, lesser are the chances of untested code getting into the release cycle.
The JaCoCo-Maven Plugin is a widely used open-source Java library for measuring and reporting code coverage during automated testing.
What Is Code Coverage and Why Is It Important?
Code coverage measures how much of your source code executes while running test suites. It helps identify untested parts of code, detect dead code, and improve software quality. High coverage often indicates well-tested code, reducing the likelihood of defects entering production.
Key Benefits:
What Is the JaCoCo-Maven Plugin?
JaCoCo (Java Code Coverage) is an open-source code coverage tool that integrates seamlessly with Maven, IDEs like Eclipse, and CI/CD platforms such as Jenkins. It collects coverage data at runtime through the JaCoCo agent, which instruments the code and generates execution reports.
Why It’s Useful:
How To Set Up the JaCoCo Plugin With Maven
Setting up JaCoCo in Maven involves adding the plugin to the project’s pom.xml file. You define goals like prepare-agent and report to configure when and how reports are generated.
Key Steps:
pom.xml.prepare-agent (records execution data) and report (generates reports).destFile and outputDirectory.mvn test to automatically produce coverage reports in target/site/jacoco/index.html.How To Analyze JaCoCo Reports
Once the report is generated, JaCoCo provides metrics for line coverage, branch coverage, and cyclomatic complexity — helping developers assess test completeness.
Color Indicators:
These insights guide developers to improve coverage by adding or refining test cases.
The JaCoCo-Maven Plugin offers an efficient and flexible solution for tracking and improving test coverage in Java projects. By integrating it into Maven builds, teams can ensure better code reliability, faster testing cycles, and higher-quality software releases.
Code Coverage is a metric used in software development to represent the extent to which an application’s source code is executed while a test suite is run. To see and evaluate the code coverage of a software application, a report is produced. Then, code quality may be ensured using this code coverage report.
The JaCoCo-Maven plugin is used to generate code coverage reports. Source code with high code coverage has more of its code executed during testing. For example, if the software you are testing contains 100 lines of code and the number of code lines validated in the software is 90, then the code coverage percentage of that software application will be 90%.
Code coverage is a very useful metric for developers, testers, and QA engineers. Here are some of the salient benefits of code coverage:
As we know, code coverage is very important for every software product. However, if you are a developer or a tester, you can cure your interest in code coverage via the article: Code Coverage vs Test Coverage, which one is better?
Now that we have done a quick recap about the integral aspects of code coverage, let’s deep dive into our core topic i.e. generating code coverage reports using Jacoco Maven plugin.
JaCoCo-Maven (abbreviation for Java Code Coverage) plugin is an open-source code coverage tool for Java. It creates code coverage reports and integrates well with IDEs(Integrated development environments) like Eclipse IDE.
It also integrates smoothly with CI/CD tools (e.g. Jenkins, Circle CI, etc.) and project management tools (e.g. SonarQube, etc.). It is a part of the Eclipse Foundation and has replaced the EclEmma code coverage tool in Eclipse.

The JaCoCo-Maven plugin is appropriate for code coverage because of the following reasons:
To get code coverage reports in a Maven project, we first need to set up the JaCoCo Maven plugin for that project. By integrating the JaCoCo plugin, the results of the code coverage analysis can be reviewed as an HTML report. The current version of the JaCoCo-Maven plugin can be downloaded from the MVN Repository.
Here are the steps to integrate JaCoCo Maven plugin with a Maven project:
1. Every Maven project has a pom.xml file, used to declare all the dependencies and plugins. The JaCoCo-Maven plugin is declared in the same POM.xml file. The XML code for the same is :
org.jacoco
jacoco-maven-plugin
0.8.6This is the basic XML code added under the build tag for specifying the JaCoCo plugin in a Maven-based project. We can enhance the functionality (like mentioning when a report should be generated, etc.) by specifying goals and rules in the execution tag.
2. After the version tag, we add the execution tag. This tag prepares the properties or execution to point to the JaCoCo agent and is passed as a VM (in this case, JVM) argument.
3. For running simple unit tests, two goals set in execution tags will work fine. The bare minimum is to set up a prepare-agent and report goals.
org.jacoco
jacoco-maven-plugin
0.8.6
prepare-agent
prepare-agent
report
test
reporttarget/jacoco-ut.exec, and the code coverage report is written to the directory target/site/jacoco/index.html.4. For running simple unit tests, the above configuration works fine. However, we would need constraints to be put on the code coverage reports (e.g. specify the destination directory, etc). This can be done via a configuration tag.
org.jacoco
jacoco-maven-plugin
0.8.6
prepare-agent
prepare-agent
${project.build.directory}/coverage-reports/jacoco-ut.exec
surefireArgLine
report
test
report
${project.build.directory}/coverage-reports/jacoco-ut.exec
${project.reporting.outputDirectory}/jacoco-utConfiguration of first execution :
As per the above code, you can see some tags like destFile, etc., are specified. Here is a brief description of the tags:
Configuration of second execution :
As per the above code, you can see tags like dataFile, etc., are specified. Here is a brief description of the tags:
5. We can also add rules to our configuration tag to keep a check on the code coverage percentage. This can be done as shown below:
org.jacoco
jacoco-maven-plugin
0.8.6
prepare-agent
prepare-agent
${project.build.directory}/coverage-reports/jacoco-ut.exec
surefireArgLine
report
test
report
${project.build.directory}/coverage-reports/jacoco-ut.exec
${project.reporting.outputDirectory}/jacoco-ut
jacoco-check
check
PACKAGE
LINE
COVEREDRATIO
0.50Configuration of third execution :
Here, a new goal check is defined. The jacoco:check goal is bound to verify the rule specified. Rule is given in the rule tag. You have the flexibility to specify more than one rule.
6. There are multiple goals and rules which can be defined in the JaCoCo-Maven plugin configuration.
In this section, we would demonstrate the steps to generate code coverage report using the JaCoCo Maven plugin. The demonstration is done by taking a very simple test scenario. So, let’s get started.
Also, watch this in-depth tutorial to learn how to measure code coverage in Java using the JaCoCo plugin.
Pre-requisites:
Steps to create a simple Maven Project:





org.jacoco
jacoco-maven-plugin
0.8.6
<!--first execution : for preparing JaCoCo runtime agent-->
prepare-agent
prepare-agent
<!--second execution : for creating code coverage reports-->
report
test
report
<!--?xml version="1.0" encoding="UTF-8"?-->
4.0.0
com.example
jacoco-example
0.0.1-SNAPSHOT
jacoco-example
UTF-8
1.7
1.7
<!-- JUnit dependencies added to run test cases -->
junit
junit
4.11
test
<!-- Maven plugin for Project Management -->
maven-clean-plugin
3.0.0
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.7.0
maven-surefire-plugin
2.19.1
maven-jar-plugin
3.0.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
org.jacoco
jacoco-maven-plugin
0.8.6
prepare-agent
prepare-agent
report
test
reportpackage com.example.Jacoco_lambdatest;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class LambdaTest {
public static String username = "";
public static String accessKey = "";
public static DesiredCapabilities setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platform", "Windows 10");
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("version", "87.0"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("resolution","1024x768");
capabilities.setCapability("build", "First Test");
capabilities.setCapability("name", "Sample Test");
capabilities.setCapability("network", true); // To enable network logs
capabilities.setCapability("visual", true); // To enable step by step screenshot
capabilities.setCapability("video", true); // To enable video recording
capabilities.setCapability("console", true); // To capture console logs
return capabilities;
}
}Adding JUnit test Cases in the project:
package com.example.Jacoco_lambdatest;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import com.example.Jacoco_lambdatest.*;
public class AppTest {
public static RemoteWebDriver driver;
@Test
public void testScript1() throws Exception {
try {
DesiredCapabilities capabilities = LambdaTest.setUp();
String username =LambdaTest.username;
String accessKey = LambdaTest.accessKey;
RemoteWebDriver driver = new RemoteWebDriver(new URL("https://"+username+":"+accessKey+"@hub.lambdatest.com/wd/hub"),capabilities); driver.get("https://lambdatest.github.io/sample-todo-app/");
driver.findElement(By.name("li1")).click();
driver.findElement(By.name("li2")).click(); driver.findElement(By.id("sampletodotext")).clear(); driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
driver.findElement(By.id("addbutton")).click();
driver.quit();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}




CEO, Vercel
Discovered @TestMu AI yesterday. Best browser testing tool I've found for my use case. Great pricing model for the limited testing I do 👏
Deliver immersive digital experiences with Next-Generation Mobile Apps and Cross Browser Testing Cloud
Our code coverage report shows 94% instruction coverage, 100% branch coverage, which is a great code coverage score. Later, we will try to achieve a 100% code coverage score by adding more test cases.
The 38 instructions shown by JaCoCo in the report refer to the bytecode instructions instead of Java code instructions. The JaCoCo reports help you visually analyze code coverage by using diamonds with colors for branches and background highlight colors for lines. A brief explanation of the diamonds seen in the code coverage report is below:
The same color code applies to the background highlight color for the line coverage. The report mainly provides three crucial metrics :
@Test
public void testScript2() throws Exception {
try {
DesiredCapabilities capabilities = LambdaTest.setUp();
String username = LambdaTest.username;
String accessKey = LambdaTest.accessKey;
RemoteWebDriver driver = new RemoteWebDriver(new URL("https://"+username+":"+accessKey+"@hub.lambdatest.com/wd/hub"),capabilities); driver.get("https://lambdatest.github.io/sample-todo-app/"); driver.findElement(By.name("li2")).click();
driver.findElement(By.name("li3")).click();
driver.findElement(By.id("sampletodotext")).clear(); driver.findElement(By.id("sampletodotext")).sendKeys("Yes, Let's add it to list"); driver.findElement(By.id("addbutton")).click();
driver.quit();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void testScript3() throws Exception {
try {
DesiredCapabilities capabilities = LambdaTest.setUp();
String username = LambdaTest.username;
String accessKey = LambdaTest.accessKey;
RemoteWebDriver driver = new RemoteWebDriver(new URL("https://"+username+":"+accessKey+"@hub.lambdatest.com/wd/hub"),capabilities); driver.get("https://lambdatest.github.io/sample-todo-app/"); driver.findElement(By.name("li4")).click();
driver.findElement(By.id("sampletodotext")).clear(); driver.findElement(By.id("sampletodotext")).sendKeys("Yes, Let's add it!");
driver.findElement(By.id("addbutton")).click();
driver.quit();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
<!--Third execution : used to put a check on the entire package-->
jacoco-check
check
PACKAGE
LINE
COVEREDRATIO
0.50
Selenium testing on the cloud helps you attain better browser coverage, increased test coverage, and accelerated time to market. Parallel testing in Selenium helps you achieve the above mentioned requirements.
TestMu AI Cloud Selenium Grid is a cloud-based scalable Selenium testing platform that enables you to run your automation scripts on 2000+ different browsers and operating systems.
Pre-requisites:
To run the test script using JUnit with Selenium, first, we need to set up an environment. You would first need to create an account on TestMu AI. Do make a note of the username and access-key that is available in TestMu AI profile section.
We will use this sample project for Java Selenium testing.
After downloading a zip file of the project: junit-selenium-sample from GitHub, we import it to Eclipse IDE by following the below mentioned steps:


<!--?xml version="1.0" encoding="UTF-8"?-->
4.0.0
com.lambdatest
lambdatest-junit-sample
1.0-SNAPSHOT
UTF-8
2.19.1
default
<!--JUnit dependency-->
junit
junit
4.12
test
commons-io
commons-io
1.3.2
test
<!--Selenium dependency-->
org.seleniumhq.selenium
selenium-java
2.52.0
test
com.googlecode.json-simple
json-simple
1.1.1
test
<!--Apache Maven Plugins-->
maven-compiler-plugin
3.0
1.8
1.8
maven-surefire-plugin
2.19.1
<!--JaCoCo Maven Plugin-->
org.jacoco
jacoco-maven-plugin
0.8.6
prepare-agent
prepare-agent
report
test
report<em><a href="https://github.com/rachnaagrawal/junit-selenium-sample/blob/master/pom.xml" target="_blank" rel="nofollow noopener">Github</a></em>
WebDriver driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + "@hub.lambdatest.com/wd/hub"),
DesiredCapabilities.firefox()); //A class named DesiredCapabilities is used to create an environment as a Firefox browser.
| Fields | Selected Values |
|---|---|
| Operating Systems | Windows 10 |
| Browser | Chrome |
| Browser Version | 62.0 |
| Resolution | 1024×768 |
| Selenium Version | 3.13.0 |

[ { "tunnelName":"LambdaTest tunnel",
"buildName":"running Java Selenium Tests",
"testName":"Jacoco JUnit Test",
"username": "user-name",
"access_key":"access-key",
"operatingSystem" : "win10",
"browserName" : "chrome",
"browserVersion" : "62.0",
"resolution" : "1024x768" }]
package com.lambdatest;
import com.lambdatest.LambdaTestBaseTest;
import org.junit.Test;
import org.openqa.selenium.By;
import static org.junit.Assert.assertEquals;
public class SimpleTest extends LambdaTestBaseTest {
/**
* Simple Test case annotation for JUnit Test
* @throws Exception
*/
@Test
public void validateUser() throws Exception {
driver.get("https://lambdatest.github.io/sample-todo-app/");
driver.findElement(By.name("li1")).click();
driver.findElement(By.name("li2")).click();
driver.findElement(By.id("sampletodotext")).clear();
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
driver.findElement(By.id("addbutton")).click();
}
}


In this article, we have seen how to use the JaCoCo-Maven plugin to generate code coverage reports for Java projects. We have also leveraged the agility and scalability of TestMu AI Selenium Grid cloud to automate the test processes. Remember, though, 100% code coverage is not responsible for reflecting effective testing, as it only shows the amount of code exercised during tests.
Yet, it helps to reduce the number of bugs and improves the software release quality. Also, it adds minimal overhead to the build process and allows it to keep a certain threshold as the development team adds edge cases or implements defensive programming.

Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance