Junit Code Coverage using Gradle, Jacoco

For your Java application you need to check the code coverage of your Junit test classes. Code coverage ensures your code quality for the Junit test cases you have written for your functionalities of the application. Here I am going to show you how you can use Jacoco plugin to test the coverage.

Currently it supports instruction, branch, line, method and class coverage which is pretty enough you can expect from this kind of tool. Additionally, it can measure and report cyclomatic complexity for methods and summarize the complexity for classes and packages.

You can also find maven based Junit Code Coverage using Jacoco library.

Prerequisites

Java at least 1.8, Junit 4.12, Jacoco 0.8.5, Gradle 6.5.1

Project Setup

You can use any IDE or tool of your choice to create gradle based project. The name of the project is java-junit-code-coverage-jacoco-gradle. make sure you have the following configuration in build.gradle script file.

plugins {
    id 'java-library'
    id 'jacoco'
}

jacoco {
    toolVersion = "0.8.5" //jacoco version
    reportsDir = file("$buildDir/reports/jacoco") //default directory where jacoco generates test reports
}

//if you want to use custom report format then uncomment below task 
//use command "gradle build jacocoTestReport" to generate reports using below task
/*jacocoTestReport {
	dependsOn test // tests are required to run before generating the report
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/reports/jacoco"
    }
}

test {
    ignoreFailures = true //build success even when test failure occurs
    finalizedBy jacocoTestReport // report is always generated after tests run
}*/

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
	testImplementation('junit:junit:4.12') {
        exclude group: 'org.hamcrest'
    }
    
    testImplementation 'org.hamcrest:hamcrest-library:2.2'
}

Java and Junit Classes

Now create below Java class that simply provides four operations, such as, add, substract, multiply and divide on two values.

package com.roytuts.java.junit.code.coverage.jacoco.gradle;

public class Calculator {

    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }

    public int divide(int a, int b) {
        return a / b;
    }

}

Create below Junit class that will test the above Calculator class.

package com.roytuts.java.junit.code.coverage.jacoco.gradle;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {

    private Calculator calculator;

    @Before
    public void setup() {
        calculator = new Calculator();
    }

    @Test
    public void testAdd() {
        int result = calculator.add(5, 4);
        Assert.assertEquals(9, result);
    }

    @Test
    public void testSubtract() {
        int result = calculator.subtract(5, 4);
        Assert.assertEquals(1, result);
    }

    @Test
    public void testMultiply() {
        int result = calculator.multiply(5, 4);
        Assert.assertEquals(20, result);
    }

    @Test
    public void testDivide() {
        int result = calculator.divide(5, 4);
        Assert.assertEquals(1, result);
    }

    @After
    public void clean() {
        calculator = null;
    }

}

Generate Test Report

Run the below command to build and execute test reports using Jacoco library. This command you can run from the command line tool on your project’s root directory.

gradlew clean build test

Your test report is generated as set in the build.gradle script. Therefore you need to navigate to directory $buildDir/reports/tests/test (actual path should be for this example – java-junit-code-coverage-jacoco-gradle\build\reports\tests\test) and open index.html to see the Junit test report as shown in the following image:

java junit jacoco code coverage

In the above image you see that I have got 100% code coverage. I had created simple Java class and it will vary based on project that has a number of Java classes and Junit test classes.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *