Introduction

Here I will show you how to configure Junit 5 using maven and Gradle in Eclipse. To get started with Junit 5, you need at least a single TestEngine implementation, for example, junit-jupiter-engine. We will show here both maven and gradle version dependencies for Junit 5.

This TestEngine will pull in all the required dependencies. Among those dependencies is junit-jupiter-api which contains the required classes and interfaces your test sources require to compile. junit-platform-engine is also resolved and added.

This is the only step that is required to get you started and you should be fine creating test classes in test source directory (e.g., src/test/java).

Prerequisites

At least Java 8, Junit 5

Maven Configuration

As usual you need to add the below dependency to your existing pom.xml file.

<dependencies>
    ...
	
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.5.2</version>
        <scope>test</scope>
    </dependency>
	
    ...
</dependencies>

The complete content for the maven build file is given below:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>junit-5-setup-maven</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.5.2</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
					<source>12</source>
					<target>12</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Gradle Configuration

If you are using gradle based configuration then you can use below build.gradle script:

plugins {
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
}

Write Small Program

We will create Java small program to test our Junit 5 configuration. Actually the below class and test class are generated by gradle build tool.

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package com.roytuts.junit.setup.gradle.maven;

public class Library {
	
    public boolean someLibraryMethod() {
        return true;
    }
    
}

Test Class:

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package com.roytuts.junit.setup.gradle.maven;

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

import org.junit.jupiter.api.Test;

import com.roytuts.junit.setup.gradle.maven.Library;

public class LibraryTest {

	@Test
	public void testSomeLibraryMethod() {
		Library classUnderTest = new Library();
		assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'");
	}

}

Testing the Program

Executing the above test class will pass the test case.

That’s all. Hope you got idea how to setup Junit 5 using maven or gradle.

Thanks for reading.

Tags:

Leave a Reply

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