Junit 5 Maven Example

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

Technologies used :

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

Maven project

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

Junit 5 Maven example

Junit 5 + Maven

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

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

Copy to Clipboard

A simple MathUtil.java

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

Copy to Clipboard

A simple Junit 5 test case

Copy to Clipboard

Run your Junit 5 test in IDE

In your IDE, right click on your Junit 5 test case, go Run As and select Junit Test. Yoor test case execution will be started.

excute junit test in IDE

Junit 5 test case execution results in IDE:


Run  Junit 5 test 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.

Console output:

Copy to Clipboard