In this post, we will learn how to configure Maven Dependency for JUnit 5 and how to use them to create and execute JUnit Tests
Note: JUnit 5 requires minimum Java 8 at runtime.
To execute JUnit 5 tests using maven we will need a minimum of two dependencies.
1. JUnit Jupiter Engine Dependency
junit-jupiter-engine has internally dependency on junit-platform-engine and junit-jupiter-api so if you add junit-jupiter-engine dependency then it will bring all three dependencies into class path.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
Junit 5 jupiter engine dependency tree
junit-jupiter-api has all JUnit annotations to write tests and extensions and junit-platform-engine has test engine implementation which is required at runtime to run the tests.
2. JUnit Platform Runner Dependency
We usually need junit-platform-runner for executing JUnit tests and test suites on the JUnit Platform environment.
As junit-platform-runner is internally dependents on junit-platform-launcher and junit-platform-suite-api so if you add junit-platform-runner dependency then it will bring all three dependencies into class path.
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
Junit 5 platform runner dependency tree
Complete JUnit 5 Maven Project Example
Project structure
Utility Class for that we have to write JUnit Test:
1 2 3 4 5 6 7 8 |
package kkjavatutorials.com; public class MyUtils { public int add (int n1,int n2) { return n1+n2; } } |
JUnit Test class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package kkjavatutorials.com; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class MyUtilsTest { @Test void addTest() { MyUtils myUtils = new MyUtils(); assertEquals(10, myUtils.add(6, 4)); } } |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<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>kkjavatutorials.com</groupId> <artifactId>JUnitExample</artifactId> <version>0.0.1-SNAPSHOT</version> <name>JUnitExample</name> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.5.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
Right-click on JUnit Class and choose Run As -> JUnit Test
The output of above JUnit Project
You May Also Like:
What is JUnit?
JUnit 5 Annotations
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
JUnit 5 @Disabled Test Example
Conditional Test Execution in JUnit
Test Execution Order in Junit 5
JUnit 5 Nested Tests Example
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5
JUnit 5 @RepeatedTest Annotation example
Junit 5 Parameterized Tests with examples
Argument Conversion in Parameterized Tests in Junit 5
Argument Aggregation in Parameterized Tests in Junit 5
Customizing Display Names of Parameterized Tests in JUnit 5
Dynamic Tests example in Junit 5
JUnit 5 Test Suites Examples
JUnit 5 Temporary Directory Support
How to execute Junit 5 tests in Eclipse IDE
JUnit 5 Test Templates for Eclipse
JUnit 5 vs JUnit 4
If you have any feedback or suggestion please feel free to drop in below comment box.
That’s all about JUnit 5 Maven Dependency
If you have any feedback or suggestion please feel free to drop in below comment box.