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
Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
Java Deprecated API Scanner tool: Java Deprecated API Scanner tool i.e. jdeprscan is a static analyzing command-line tool which is introduced in JDK 9 for find out the uses of deprecated API in the given input. Here the input can be .class file name, directory or JAR file. Whenever we provide any in
2 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
How to Rerun the Failures in a BDD Junit Test Automation Framework?
We come across failures many times after we execute our test scenarios through automation. These failures can be because of majorly two reasons: Functional Issue (actual failure).Intermittent Issues (Application Slowness, Page not loaded properly, Technical Issue, etc.). Having a mechanism to automa
7 min read
Encode and Decode Strings in Java with JUnit Tests
Strings are very useful and they can contain sequences of characters and there are a lot of methods associated with Strings. Moreover, encoding and decoding are possible for a given use case and it can be validated whether are they equal or not for a given requirement. They can be tested for validit
4 min read
Java class dependency analyzer in Java 8 with Examples
Java class dependency analyzer: jdeps is a new command-line tool introduced in JDK 8 to understand the static dependencies and libraries of application i.e. jdeps command shows the package-level or class-level dependencies of Java class files. The input for jdeps can be a .class file pathname, a JAR
2 min read
javap tool in Java with Examples
javap tool The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used (â-câ or â-verboseâ for byte code and byte code along with innards info, respective
2 min read