JaCoCo Java Code Coverage Tool

JaCoCo Java Code Coverage Tool

Introduction to JaCoCo Java Code Coverage Tool

Code Coverage as the name suggests is the technique of measuring how much source code is covered or executed while your test cases are running. It can help you to understand how much of your source code are tested so that you can determine the quality of your test suites and rewrite such test cases if required. One or more code coverage criteria or metric will be used by code coverage tools to check how much code was executed during automated test suite run. Let’s learn everything about JaCoCo Java Code Coverage Tool.

Code Coverage criteria

Some of these criteria are listed below.

Line coverage: How many lines of source code are covered by set of test cases

Function coverage: How many functions defined have been called.

Condition coverage: How many Boolean expressions are evaluated/tested for true and false value.

Loop coverage: Out of all loops written, has every possible loop been executed at least one time or multiple times.

Branch coverage: How many of branch statement of control structure (if and else) have been executed.

Based on the above code coverage criteria, code coverage tool generates a report after running all your unit test cases showing how much percentage of source code has been executed. It precisely shows what lines were executed and what lines were not executed with the help of some color coding.

What “percentage” in your code coverage report represents?

If the report is closer to 100%, you have better chances of having bug free code. Let’s say if code coverage report shows 70%, it means that 30% of source code were not executed by the test cases you have written. So with the report in your hand, you can actually analyse what 30% code has not been covered and thus you can modify your unit test cases to include the remaining 30%. You might be thinking that what is stopping you to get the 100% code coverage report, you can refer the following example.

[code lang=”java”]
//this block of code will be executed
// this block of code will be executed

It is clear that if you have written the test case that always executes the “true” condition, then else part of the condition is not covered and is thus reflected in your coverage report in terms of percentage. So code coverage report gives you the chance to think and analyse your code in different angle. However having 100% in you code coverage report, in no way guarantee that you code is bug free or perfect. It means every line of your source code is tested but it may not be tested in every possible situation or scenario. So you should write the comprehensive unit test cases.

I hope now you are clear with what code coverage is and what its importance in project life cycle are. There are many such code coverage tools in java ecosystem like JCov, JaCoCo, Clover, OpenClover, Cobertura, EMMA etc. You can check various other code coverage tools in java along with its features in Wikipedia.

JaCoCo demonstration with maven plugin

Today I am going to demonstrate about JaCoCo, which is popularly used code coverage tool and used frequently by java developers.

JaCoCo is an open-source toolkit/library for measuring and reporting java code coverage. It offers instructions, line and branch coverage criteria.

We use see how to use JaCoCo maven plugin and generate a simple code coverage report.

Tools and Technologies used:

Java 8

Eclipse Neon

Maven plugin for Eclipse: M2E Settings 2.0.4

Kindly follow the below given steps to get started building maven project and generate code coverage report using JaCoCo maven plugin.

Step 1: Create Maven Project in Eclipse

In order to create a maven project Click File menu -> New -> Maven Project

New Maven Project dialog appears as the following

Hit Next button and select maven-archetype-quickstart as shown in the following.

Again hit the Next button and enter the project details as the following.

When Finish button in clicked, you are done with creating your maven project. Rename the class as “ArithmeticOperation” under package “com.codeAdda.BasicMathOperation” and test class as “ArithmeticOperationTest” under package “com.codeAdda.test”. Project directory structure finally looks like the following.


Project Structure: BasicMathOperation

ArithmeticOperation.java: class containing basic arithmetic operations

ArithmeticOperationTest: Junit test class

pom.xml: junit and jacoco dependencies

Step 2: Configure dependencies in pom.xml

[code lang=”xml”]
<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">





<!– Sets the path to the file which contains the execution data. –>

<!– Sets the output directory for the code coverage report. –>

Step 3:Copy the following code in ArithmeticOperation.java

[code lang=”java”]

package com.codeAdda.BasicMathOperation;

* This class contains method to perform basic arithmetic operation
* @author Chandra Mani Gupta
* @since 9/4/2018
public class ArithmeticOperation
public int add(int a, int b) {
return a+b;

public int sub(int a, int b) {
return a-b;

public int mul(int a, int b) {
return a*b;

public int div(int a, int b) {
return a/b;


Step 4: Copy the following code in ArithmeticOperationTest.java

[code lang=”java”]

package com.codeAdda.test;
import static org.junit.Assert.assertEquals;
import com.codeAdda.BasicMathOperation.ArithmeticOperation;
import org.junit.Test;

* Unit test for ArithmeticOperation.
public class ArithmeticOperationTest{

public void testAdd() {
ArithmeticOperation basic = new ArithmeticOperation();
int addResultAcutal = basic.add(2, 4);
int addResultExpected = 6;
assertEquals(addResultExpected, addResultAcutal);

public void testSub() {
ArithmeticOperation basic = new ArithmeticOperation();
int subResultAcutal = basic.sub(7, 4);
int subResultExpected = 3;
assertEquals(subResultExpected, subResultAcutal);

public void testMul() {
ArithmeticOperation basic = new ArithmeticOperation();
int mulResultAcutal = basic.mul(2, 4);
int mulResultExpected = 8;
assertEquals(mulResultExpected, mulResultAcutal);

public void testDiv() {
ArithmeticOperation basic = new ArithmeticOperation();
int divResultAcutal = basic.div(8, 4);
int divResultExpected = 2;
assertEquals(divResultExpected, divResultAcutal);


Step 5: Run the project
Right Click on BasicMathOperation->Run As -> Maven Test

Now maven starts downloading all dependencies related to junit and jacoco. You should have a internet connection with good speed so that all the artifacts gets downloaded without any problem. When all artifacts are downloaded, you will get build success message as following in eclipse console.

Now you get the report generated in html format in ..\BasicMathOperation\target\jacoco-ut folder.

Double click on index.html to open the code coverage report and navigate to ArithmeticOperation to see the full report.

When you click on any of the given method, you will get the detailed report. In this report code in the green color shows that it as been covered while executing unit test cases.

Since you have came so far, i suppose you are enjoying this article. In case you run into any problem, feel free to ping me in comments below. Happy Coding!!

1 Comment

Leave a Reply