You can find maven based Junit Code Coverage https://www.jeejava.com/junit-code-coverage/ This tutorial will show you Gradle based Junit Code coverage.

Prerequisites

IntelliJ IDEA
Gradle 3.5
Java 1.8

Please check how to create Gradle Project in IntelliJ IDEA

build.gradle file

group 'com.jeejava'  //group id
version '1.0-SNAPSHOT' //version

apply plugin: 'java'
apply plugin: 'jacoco' //apply jacoco plugin for using jacoco

sourceCompatibility = 1.8
targetCompatibility = 1.8

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

//if you want to use custom report then uncomment below task //use command "gradle build jacocoTestReport" to generate reports using below task

/*jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}*/

//build success even when test failure occurs
test {
    ignoreFailures = true
}

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        mavenLocal()
    }
}

repositories {
    mavenCentral()
}

//junit and hamcrest libraries
dependencies {
    compile 'org.slf4j:slf4j-api:1.7.5'
    testCompile('junit:junit:4.12') {
        exclude group: 'org.hamcrest'
    }
    testCompile 'org.hamcrest:hamcrest-library:1.3'
}

Now create below Java class

package jeejava;

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 test class

package jeejava;

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;
    }

}

Now run the below command to build and execute test reports using jacoco

gradle build test

Now navigate to directory “$buildDir/reports/tests/test” and open index.html to see the Junit test reports

For more information you may read https://docs.gradle.org/3.3/userguide/jacoco_plugin.html

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

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