Home Junit 5 Junit 5 Maven configuration with example

Junit 5 Maven configuration with example

In this Junit 5 maven article we will see how to configure Junit 5 dependencies in your Maven projects and how to run over maven commands and from your IDE like eclipse or STS (Spring tool suite).

1. Example application

1.1. Technologies used :

  1. Junit 5.5.2
  2. Maven 3
  3. Java 8
  4. Spring Tool Suite 3.9.8

1.2. Maven project

Here is the simple maven project structure to demonstrate Junit 5 and Maven configuration.

Junit 5 Maven example

2. Add Junit 5 dependencies to your pom.xml

junit-jupiter-engine an api to create  Junit 5 test cases.

Add junit-jupiter-engine maven dependency and maven-surefire-plugin maven plugin to maven pom.xml. Your pom.xml looks like below.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



		<!-- Dependency versions -->

		<!-- Java 8 -->

		<!-- Encoding -->

	<!-- Jupiter API for writing tests -->

	<!-- Maven Surefire plugin to run tests -->
		   <!-- plugin to run test cases from maven -->
			<!-- Maven plugin to use perticular java version to compile code -->


3. A simple MathUtil.java to test with Junit 5

A simple MathUtil.java class with basic methods to calculate math functions.

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;

4. Writing Junit 5 tests

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

import org.junit.jupiter.api.Test;

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() {

5. Run your Junit 5 test in IDE

5.1. In your IDE Eclipse / STS, right click on your Junit 5 test case, go Run As and select Junit Test. Your test case execution will be started.

excute junit test in IDE

5.2. Junit 5 test case execution results in IDE :


6. Run using maven command

Run mvn test maven command, the Maven Surefire Plugin will scan and run all the Juint 5 test cases and you see the test cases execution results in console.

6.1. Console Output

[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ Junit5-maven-example ---
[INFO] -------------------------------------------------------
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.util.MathUtilTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.048 s - in com.javabydeveloper.util.MathUtilTest
[INFO] Results:
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.125 s
[INFO] Finished at: 2019-12-27T12:14:23+08:00
[INFO] ------------------------------------------------------------------------

You also might be interested in following examples :

  1. Junit 5 Dynamic Tests and @TestFactory annotation.
  2. Junit 5 tags and filter test cases for execution.
  3. Junit 5 Timeout tests, fail tests if not completed within time.
  4. Junit 5 Display Names for tests.
  5. Junit 5 parameterized tests with different argument sources.
  6. Junit 5 Disable tests using @Disabled annotation

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. Follow him on LinkedIn or Twitter or Facebook


Please enter your comment!
Please enter your name here

Stay in Touch