Open In App

JaCoCo with IntelliJ IDEA

Last Updated : 04 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 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 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 produces 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 use JaCoCo with IntelliJ to gather coverage reports on unit tests.

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. 

After successfully creating a sample project to check the code coverage inside the IntelliJ IDEA you have to follow the following steps. 

Step 2: Go to the Run > Edit Configurations… as shown in the below image. 

 

Step 3: In the next step you have to create a new configuration for JUnit. Refer to the below image for detail. 

 

Step 4: In the final step select JaCoCo from the Choose coverage runner dropdown as shown in the below image. And you are done. 

 

Now, let’s test whether it’s working or not. Go to the src > test > java > right-click and click on the Run ‘All Tests’ with Coverage. Refer to the below image if you are stuck somewhere.

 

And if everything is okay then you can see the whole coverage report on the right side something like the below image. 

 

And if you open any specific class you can see which lines are covered and which lines are not covered like the below image.

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads