Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Generate Code Coverage Report with JaCoCo in Java Application?
Next article icon

How to Generate Code Coverage Report with JaCoCo in Java Application?

Last Updated : 07 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Create a simple Java application
  2. Jot down some test cases inside the application
  3. Add the JaCoCo maven plugin
  4. Update code after adding the JaCoCo plugin
  5. After adding the dependency click on the Maven option
  6. Select clean and test
  7. Select the run button (Green color triangle)
  8. 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. 


 


Next Article
How to Generate Code Coverage Report with JaCoCo in Java Application?

A

AmiyaRanjanRout
Improve
Article Tags :
  • Java
  • java-advanced
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences