How to Generate Code Coverage Report with JaCoCo in Java Application?
Last Updated :
07 Jul, 2022
Testing is a most important part of a software development lifecycle. Without testing, the software is not ready for deployment. To test Java applications we mostly used Junit. JUnit framework is a Java framework that is used for testing. And now, JUnit is used as a standard when there is a need to perform testing in Java. But what is Code Coverage and what is JaCoCo? Code Coverage is a software metric that is used to measure how many lines of our code are executed during automated tests. In other words, we can also say Code Coverage describes the percentage of code covered by automated tests and it checks which parts of code run during the test suite and which don’t.
JaCoCo stands for Java Code Coverage. It is a free code coverage library for Java, which has been created by the EclEmma team. It creates code coverage reports and integrates well with IDEs like IntelliJ IDEA, Eclipse IDE, etc. JaCoCo also integrates with CI/CD tools like Jenkins, Circle CI, etc., and project management tools like SonarQube, etc. So in this article, we are going to create a sample Java application and generate the code coverage report with the help of the JaCoCo maven plugin.
Procedure:
- Create a simple Java application
- Jot down some test cases inside the application
- Add the JaCoCo maven plugin
- Update code after adding the JaCoCo plugin
- After adding the dependency click on the Maven option
- Select clean and test
- Select the run button (Green color triangle)
- Navigate for code coverage.
Step by Step Implementation
Step 1: Create a simple Java application and write down some test cases inside the application using Junit or you can also use Mockito.
Related article: You may refer to this article How to Write Test Cases in Java Application using Mockito and Junit? and create a sample project.
Step 2: Add the JaCoCo maven plugin to your pom.xml file.
The plugins for the JaCoCo is given below is as follows:
XML
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Below is the updated code for the pom.xml file after adding the JaCoCo plugin.
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mockito-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Step 3: After adding the dependency click on the Maven option which is present on the right upper side as shown in the below image. Then select clean and test and then select the run button (Green color triangle).

Now you can see your test result as shown in the below image. And your code coverage report has been generated. But where? Refer to step 4.

Step 4: To get you code coverage report navigate to the target > site > jacoco > index.html > right-click > Open In > Browser > And your preferred browser. So basically index.html is your code coverage report file.

So you can see your report will be shown like this and the percentage completely depends on how you have written the test cases.

Note: The green color shows that all lines of code have been covered which means you have written the test cases for all the units. If you have encountered the yellow color line then partial code has been covered and if you have encountered with the red color then the code has not been covered.

Similar Reads
How to Write Test Cases in Java Application using Mockito and Junit? Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. Unit Testing is a typ
4 min read
How to Generate HTML Report from Cucumber JSON File in BDD Framework? In a Test Automation Framework, itâs very important to have a Summary Report of test execution. There are many report options such as Cucumber Report, Extent Report, etc. which are pretty detailed reports, but most of the time these reports are generated either inside the CI/CD tool (i.e. Jenkins, e
9 min read
JUnit 5 â JaCoCo Code Coverage In simple terms, code coverage means measuring the percentage of lines of code that are executed during automated tests. For example, if you have a method containing 100 lines of code and you are writing a test case for it, code coverage tells you briefly how many of those lines were actively exerci
5 min read
Generate Junit Test Cases Using Randoop API in Java Here we will be discussing how to generate Junit test cases using Randoop along with sample illustration and snapshots of the current instances. So basically in Development If we talk about test cases, then every developer has to write test cases manually. Which is counted in the development effort
4 min read
How to Test Java Application using TestNG? TestNG is an automation testing framework widely getting used across many projects. NG means âNext Generationâ and it is influenced by JUnit and it follows the annotations (@). Â End-to-end testing is easily handled by TestNG. As a sample, let us see the testing as well as the necessity to do it via
4 min read
JUnit5 - Map Assertions With AssertJ with Example Maven Project In this article, by seeing a sample project that involves a few characters in the "friends" web series and checking their names against correct professions as fun. For storing names and their professions, we need a "HashMap" facility. In that let us store their names and professions. With that, we n
4 min read
How to Generate JVM Heap Memory Dump? Java Heap dump is a snapshot of all java objects that are present in the JVM(Java Virtual Machine) at a certain point in time. The JVM allocates memory for objects which are class instances or arrays in the heap memory. When the objects are no longer needed or are no more referenced, the Garbage Col
7 min read
How to Test API with REST Assured? REST Assured is a Java library that provides a domain-specific language (DSL) for writing powerful, easy-to-maintain tests for RESTful APIs. It allows you to specify the expectations for HTTP responses from a RESTful API, and it integrates seamlessly with JUnit, the most popular testing framework fo
5 min read
JaCoCo with IntelliJ IDEA Testing is a most essential part of a software development lifecycle. Without testing, the software is not ready for deployment. To test Java applications we mostly used Junit. JUnit framework is a Java framework that is used for testing. And now, JUnit is used as a standard when there is a need to
3 min read
Introduction to Checkstyle Plugin for Checking Java Code Quality Checkstyle is a development tool to help programmers to write Java code that sticks to a coding standard. It automates the process of checking Java code. It is an open-source tool that checks code against a configurable set of rules. It allows you to define your own set of rules and check your code
5 min read