To execute Junit 5 tests in eclipse, we need to add the following dependencies in with test scope in pom.xml
1.junit-platform-runner : provides the location of the JUnitPlatform runner
2.junit-jupiter-api :API for writing tests, including @Test, etc. It is transitively included when we include JUnit-Jupiter-engine.
3.junit–jupiter-engine :It is the implementation of the TestEngine API for JUnit Jupiter
Let’s try to understand the above concept using a demo project
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5ExecuteTestsInEclipse</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> <junit.platform.version>1.6.2</junit.platform.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> </project> |
MyUtils.java
1 2 3 4 5 6 7 8 |
package com.kkjavatutorials; public class MyUtils { public int add(int n1, int n2) { return n1+n2; } } |
MyUtilsTest.java
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 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @RunWith(value = JUnitPlatform.class) public class MyUtilsTest { private static MyUtils myUtils; @BeforeAll static void setUp() { myUtils = new MyUtils(); } @Test void test_add_two_positive_numbers() { int actualResult = myUtils.add(20, 10); assertEquals(30, actualResult); } @Test void test_add_two_negative_numbers() { int actualResult = myUtils.add(-20, -10); assertEquals(-30, actualResult); } @Test void test_add_one_positive_and_one_negative_number() { int actualResult = myUtils.add(20, -10); assertEquals(10, actualResult); } @AfterAll static void tearDown() { myUtils = null; } } |
Right-click on the project and go to Run As then Maven test as shown below image
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
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
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5
That’s all about How to execute Junit 5 tests in Eclipse IDE
If you have any feedback or suggestion please feel free to drop in below comment box.