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 :
- Junit 5.5.2
- Maven 3
- Java 8
- Spring Tool Suite 3.9.8
1.2. Maven project
Here is the simple maven project structure to demonstrate Junit 5 and Maven configuration.
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" 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.javabydeveloper</groupId> <artifactId>Junit5-maven-example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Junit5-maven-example</name> <url>https://javabydeveloper.com</url> <properties> <!-- Dependency versions --> <junit.jupiter.version>5.5.2</junit.jupiter.version> <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> <!-- Java 8 --> <java.version>1.8</java.version> <!-- Encoding --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <!-- Jupiter API for writing tests --> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> <!-- Maven Surefire plugin to run tests --> <build> <plugins> <!-- plugin to run test cases from maven --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> </plugin> <!-- Maven plugin to use perticular java version to compile code --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
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++) { if(num%i==0) 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 { @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)); } }
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.
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] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ Junit5-maven-example --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [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] [INFO] Results: [INFO] [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [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 :
- Junit 5 Dynamic Tests and @TestFactory annotation.
- Junit 5 tags and filter test cases for execution.
- Junit 5 Timeout tests, fail tests if not completed within time.
- Junit 5 Display Names for tests.
- Junit 5 parameterized tests with different argument sources.
- Junit 5 Disable tests using @Disabled annotation
- Maven Skip Tests