JUnit 5 with Gradle Setup

Tags: JUnit 5

In this article we will setup a Java project that using JUnit 5 and Gradle build.

Set up Gradle build

The Gradle version we will use is 5.4.1 which build.gradle as below.

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.4.2')
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

Adding a simple test class to test add() method of BigInteger class

package simplesolution.dev;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.math.BigInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class BigIntegerTests {

    @Test
    @DisplayName("1 + 2 = 3")
    public void addTwosBigIntegerNumber() {
        BigInteger number1 = new BigInteger("1");
        BigInteger number2 = new BigInteger("2");
        BigInteger result = number1.add(number2);
        assertEquals(3, result.intValue(), "1 + 2 = 3");
    }
}

The final project structure as below.

JUnit 5 with Gradle Setup

Run test Gradle task

Run this command in your terminal to execute the test.

gradle test

Output of Gradle test task:

> Task :test

simplesolution.dev.BigIntegerTests > addTwosBigIntegerNumber() PASSED

BUILD SUCCESSFUL in 8s
2 actionable tasks: 2 executed

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/JUnit5WithGradle

or download at:

Download Source Code

Happy Coding 😊