Home gradle Junit 5 with gradle configuration with an example

Junit 5 with gradle configuration with an 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 {
    useJUnitPlatform()
}

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 dependency on the JUnit Jupiter API and define the useJUnitPlatform() like the following .
- Advertisement -
plugins {
    id 'java'
    id 'eclipse'
}

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

sourceCompatibility = 1.8

test {
    useJUnitPlatform()
}

repositories {
    mavenCentral()
}

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 {
    testImplementation("junit:junit:4.13")
    testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.6.0")
}

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++) {
	        if(num%i==0)
	            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 {
	
	@Test
	void test_Add() {
		assertEquals(5, MathUtil.add(3, 2));
	}
	
	@Test
	void test_Multiply() {
		assertEquals(15, MathUtil.multiple(3, 5));
	}

	@Test
	void test_Devide() {
		assertEquals(5, MathUtil.devide(25, 5));
	}
	
	@Test
	void testIs_Prime() {
		assertTrue(MathUtil.isPrime(13));
	}
}

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)

BUILD SUCCESSFUL in 21s
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

BUILD SUCCESSFUL in 2s
3 actionable tasks: 3 executed

Conclusion

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

References

  1. Junit-5 samples github
  2. Junit 5 document
  3. Gradle System Properties.
  4. Create gradle project in eclipse.

- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles