HomeGradleJunit 5 + gradle example

Junit 5 + gradle example

In this article, we will see a java project configuration for Junit 5 with gradle example and writing basic Junit 5 tests and you will see gradle native support for executing Junit 5 tests.

1. Junit 5 with gradle example

Since gradle 4.6 version, gradle provides native support for executing tests on the Junit platform. To enable it, you just need to specify useJUnitPlatform() within a test task declaration in build.gradle .

test {

1.1. Junit 5 with gradle Project structure

Following is the a java gradle example project structure.

junit 5 with gradle example

Technologies used :

  1. Junit 5.6.0
  2. Gradle 6.1.1
  3. Java 8
  4. Spring Tool Suite 3.9.8

1.2. build.gradle configuration

  1. To configure support for JUnit Jupiter based tests, configure a testImplementation Junit 5 gradle dependency on the JUnit Jupiter API and define the useJUnitPlatform() like the following .
plugins {
    id 'java'
    id 'eclipse'

group 'com.javabydeveloper'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

test {

repositories {

dependencies {
    testImplementation group: 'org.junit.jupiter', 
    name: 'junit-jupiter', version: '5.6.0'

The JUnit Platform can run JUnit 4 based tests as long as you configure a testImplementation dependency on JUnit 4 and a testRuntimeOnly dependency on the JUnit Vintage TestEngine implementation similar to the following.

dependencies {

1.3. A simple MathUtil to test with Junit 5

A simple MathUtil.java to perform basic math operations.

public class MathUtil {
	public static int add(int num1, int num2) {
		return num1 + num2;
	public static int multiple(int num1, int num2) {
		return num1 * num2;
	public static boolean isEven(int num) {
	    return num%2 == 0;
	public static int devide(int num, int by) {
		return num/by;
	public static boolean isPrime(int num) {
		for(int i=2; 2*i<num; i++) {
	            return false;
	    return true;

1.4. Writing unit tests for MathUtil operation in Junit 5

Following is a simple Junit 5 tests case to test MathUtil.java methods and simple Assertions.

public class MathUtilTest {
	void test_Add() {
		assertEquals(5, MathUtil.add(3, 2));
	void test_Multiply() {
		assertEquals(15, MathUtil.multiple(3, 5));

	void test_Devide() {
		assertEquals(5, MathUtil.devide(25, 5));
	void testIs_Prime() {

1.5. Build and run Gradle tests

Run the tests form command line.

$ cd to project directory
$ gradle clean test

Starting a Gradle Daemon (subsequent builds will be faster)

4 actionable tasks: 4 executed

1.6. Run Gradle tests in eclipse

We can run the tests from eclipse as well, to run tests in eclipse navigate to Gradle Tasks expand the verification folder and then right click on test task then select Gradle tasks option, following image illustrates how to run gradle tests.

run junit 5 gradle tests in eclipse

Output :

You will see following output in eclipse console.

Gradle Version: 6.1.1
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: test

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test

3 actionable tasks: 3 executed

2. Conclusion

In this article we have covered Junit 5 with gradle project setup and running gradle build task to execute test cases.

You also might interested in our other Gradle Tutorials:

  1. How to create Gradle project in eclipse
  2. Gradle – System Properties
  3. Gradle – run specific tests
  4. Gradle – Skip Tests
  5. which method is used to attach a predicate to a skipping task
  6. Junit 5 run tag-specific tests using gradle

3. References

  1. Junit-5 samples github
  2. Junit 5 document
  3. Create gradle project in eclipse


Please enter your comment!
Please enter your name here