JUnit 5 is an upgrade of JUnit 4 with an entirely separate codebase. It brings the following main features:
Java 8 Support The new assert methods now uses functional parameters hence utilizing Java 8 lambdas for testing. JUnit 5 requires Java 8 (or higher) runtime. The code compiled in previous version of Java can still run with JUnit 5.
Modules Unlike previous versions, JUnit 5 is composed of several different modules:
- JUnit Platform: It is JUnit core which allows test execution.
- JUnit Jupiter: JUnit 5 API (new) and a combination of new programming model and extension model for writing tests.
- JUnit Vintage: It provides a TestEngine which bridges JUnit 3/JUnit 4 tests to JUnit 5 platform. This means previous versions of JUnit can coexist with JUnit 5.
Extension Model JUnit 5 is a complete redesign. In contrast to the older Runner, @Rule, and @ClassRule concepts, the JUnit Jupiter extension model consists of a single, coherent concept: the Extension API. The new model resolves various shortcomings of older versions.
We will explore above features in details in future tutorials. Following is a JUnit 5 quick getting started example.
Example
Maven Dependency
pom.xml<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>6.0.1</version>
</dependency>
The above dependency also pulls the JUnit 5's platform and Jupiter API dependencies.
A Java class to test
package com.logicbig.example;
public class Calc {
public int multiply(int a, int b) {
return a * b;
}
}
Writing tests
package com.logicbig.example;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CalcTest {
@Test
public void testMultiply() {
Calc c = new Calc();
int x = 3;
int y = 5;
int expected = 15;
int actual = c.multiply(x, y);
Assertions.assertEquals(expected, actual,
() -> String.format("%s * %s != %s", x, y, expected));
}
}
As seen above, the utility class Assertions has replaced the old org.junit.Assert class. Assertions has various methods which accept Java 8 functional parameters (lambdas). We can see the advantage of initializing the error message lazily (only when the assertion fails) by using Java 8 lambda. Following is the method definition which we used above:
public static void assertEquals(int expected,
int actual,
Supplier<String> messageSupplier)
Running the test
Running in Intellij
Running in maven
mvn test -Dtest=CalcTest OutputD:\example-projects\junit-5\junit-5-getting-started>mvn test -Dtest=CalcTest [INFO] Scanning for projects... [INFO] [INFO] ------------< com.logicbig.example:junit-5-getting-started >------------ [INFO] Building junit-5-getting-started 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-getting-started --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-getting-started\src\main\resources [INFO] [INFO] --- compiler:3.3:compile (default-compile) @ junit-5-getting-started --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\example-projects\junit-5\junit-5-getting-started\target\classes [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-getting-started --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-getting-started\src\test\resources [INFO] [INFO] --- compiler:3.3:testCompile (default-testCompile) @ junit-5-getting-started --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\example-projects\junit-5\junit-5-getting-started\target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-getting-started --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.CalcTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 s -- in com.logicbig.example.CalcTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.961 s [INFO] Finished at: 2025-11-29T08:23:46+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|