Generate Java Unit Test Code Coverage Report with JaCoCo in Gradle

Mi Guoliang

Introduction

Code Coverage Report is critical for Java developers to measure their unit test work. Otherwise, developers know nothing about how many logic branches and codes are tested and how many not. Developers can found missing codes easily in a code coverage report, and a code coverage report can be a clue to help them improves their unit test work.

This article is a guide to help Java developers generate and read a code coverage report from zero. And I will use Visual Studio Code (VSCode) as the development tool, and you can use your favorite IDE or editors.

Install Gradle

Gradle is an open-source build automation tool focused on flexibility and performance. Gradle can be a alternative to Maven.

If you are working on macOS, you must know about a famous package management tool named Homebrew, but the version of Gradle is old in Homebrew repository. Therefore, I use SDKMAN to install Gradle globally.

sdk install gradle

Note: The latest Gradle version is 6.2.2 now.

Otherwise, you can learn other installation ways in Gradle Installation Guide.

Create an empty Java Project

Firstly, make an empty folder named gradle-jacoco and we will work in it.

mkdir gradle-jacoco
cd gradle-jacoco

Secondly, run Gradle setup command.

$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Swift
Enter selection (default: Java) [1..5] 3

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 4

Project name (default: gradle-jacoco):
Source package (default: gradle.jacoco):

> Task :init
Get more help with your project: https://docs.gradle.org/5.6.2/userguide/tutorial_java_projects.html

BUILD SUCCESSFUL in 27s
2 actionable tasks: 2 executed

Note: We use JUnit Jupiter (JUnit 5) in this guide.

Finally, a simple Java application project is generated.

Setup JaCoCo

Open build.gradle, and append id jacoco into the plugin block.

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building a CLI application.
    id 'application'
    id 'jacoco' // HERE!
}

Write a Simple Function to be Tested Later

Open and edit App.java.

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package gradle.jacoco;

public class App {

    // To be tested!
    public String getAnAnimal(String animal) {

        if ("cat".equalsIgnoreCase(animal)) {
            return "Garfield";
        } else if ("dog".equalsIgnoreCase(animal)) {
            return "Odie";
        } else {
            return "Opps!";
        }
    }

    public String getGreeting() {
        return "Hello world.";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
    }
}

Write a Unit Test Method

Open and edit AppTest.java

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package gradle.jacoco;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AppTest {

    // Our test here!
    @Test
    void testGetAnAnimal() {
        assertEquals("Garfield", new App().getAnAnimal("cat"));
    }

    @Test
    void appHasAGreeting() {
        App classUnderTest = new App();
        assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
    }
}

Run and Generate Code Coverage Report

Use the following command to clean the previous build result, run unit tests, and generate a code coverage report.

./gradlew clean test jacocoTestReport

The final code coverage report is saved in build/reports/jacoco/test/html. Developers can open index.html to open the report in a browser.

Besides, developers can locate to uncovered lines in a specific file easily.

Note: The Code Coverage Report is generated when all unit tests pass, in another words, you can not find the code coverage report if any unit test fails.

Conclusion

Developers should know how to measure their work, including progress, quality, and efficent. The code coverage report is useful, because it can make developers’ review more effecient and effective. Therefore, developers can control their code completely with a code coverage report. However, 100% code coverage is ideal, especially it is impossible when you use some third-party library in your project, so that developers do not to be demanding.

If you like my share, you can:

• Follow My Programming Experiences Page on Facebook.

• Follow My Twitter.

• Subscribe to my mail list.