How to Generate Code Coverage Report with JaCoCo in Java Application?
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.
- 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:
Below is the updated code for the pom.xml file after adding the JaCoCo plugin.
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.