Dynamic tests do not support @BeforeEach or @AfterEach.
The execution lifecycle of a dynamic test is quite different than it is for a standard @Test case. Specifically, there are no lifecycle callbacks for individual dynamic tests. This means that @BeforeEach and @AfterEach methods callbacks are executed for the @TestFactory method but not for each dynamic test.
A common workaround is to invoke setup logic directly inside the dynamic test executable.
Example
package com.logicbig.example;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.Executable;
import java.util.Map;
import java.util.stream.Stream;
class DynamicTestsLifecycleTest {
@BeforeAll
static void beforeAll(){
System.out.println("before all main");
}
@AfterAll
static void afterAll(){
System.out.println("after all main");
}
@BeforeEach
void beforeEach(){
System.out.println("before each main");
}
@AfterEach
void afterEach(){
System.out.println("after each main");
}
@Test
void normalTest(){
System.out.println("normal test");
}
@TestFactory
Stream<DynamicTest> dataDrivenTests() {
Map<String, String> data = loadDynamicData();
return data.entrySet()
.stream()
.map(DynamicTestsLifecycleTest::toDynamicTest);
}
private static DynamicTest toDynamicTest(Map.Entry<String, String> data) {
return DynamicTest.dynamicTest(data.getKey(),
new MyTestExecutable(data));
}
private static class MyTestExecutable implements Executable {
private final String name;
private final String data;
public MyTestExecutable(Map.Entry<String, String> entry) {
name = entry.getKey();
data = entry.getValue();
}
@Override
public void execute() {
//@BeforeEach workaround
setup();
//performing test
performTest();
//@AfterEach workaround
teardown();
}
private void setup() {
System.out.println("setting up dynamic test: " + name);
}
private void performTest() {
System.out.println("Performing dynamic test: " + name);
Assertions.assertEquals(3, data.length());
}
private void teardown() {
System.out.println("tearing down dynamic test: "+name);
}
}
private static Map<String, String> loadDynamicData() {
//simulate loading data dynamically
return Map.of("A", "one", "B", "two");
}
}
Output$ mvn test -Dtest=DynamicTestsLifecycleTest [INFO] Scanning for projects... [INFO] [INFO] --< com.logicbig.example:junit-5-dynamic-tests-lifecycle-limitations >-- [INFO] Building junit-5-dynamic-tests-lifecycle-limitations 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-dynamic-tests-lifecycle-limitations --- [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-dynamic-tests\junit-5-dynamic-tests-lifecycle-limitations\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-dynamic-tests-lifecycle-limitations --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-dynamic-tests-lifecycle-limitations --- [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-dynamic-tests\junit-5-dynamic-tests-lifecycle-limitations\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-dynamic-tests-lifecycle-limitations --- [INFO] Changes detected - recompiling the module! :source [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! [INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-dynamic-tests-lifecycle-limitations --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- before all main before each main normal test after each main before each main setting up dynamic test: B Performing dynamic test: B tearing down dynamic test: B setting up dynamic test: A Performing dynamic test: A tearing down dynamic test: A after each main after all main [INFO] +--com.logicbig.example.DynamicTestsLifecycleTest - 0.145 ss [INFO] | +-- [OK] normalTest - 0.044 ss [INFO] | +-- [OK] dataDrivenTests() B - 0 ss [INFO] | '-- [OK] dataDrivenTests() A - 0 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.156 s [INFO] Finished at: 2025-12-28T09:00:25+08:00 [INFO] ------------------------------------------------------------------------
Output Analysis
The output confirms that setup logic executes explicitly within each dynamic test, compensating for missing per-test lifecycle callbacks.
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
|