The method annotated with
@TestFactory must return a
DynamicNode is abstract. It has two subclasses,
DynamicContainer is the container of other
DynamicTest is a generated test case.
DynamicTest is created from a display name and an
Executable is a functional interface that contains only one method
DynamicContainer is created from a display name and a collection of
The code below shows a simple test factory.
DynamicTest.dynamicTest(String displayName, Executable executable) is the static method to create
DynamicTest has another static method
stream(Iterator<T> inputGenerator, Function<? super T, String> displayNameGenerator, ThrowingConsumer<? super T> testExecutor) to create a
DynamicTests. The method
stream requires three parameters. The first parameter is an
Iterator<T> object that generates the input values. Each input value will be mapped to a generated test case. The second parameter is a
Function<? super T, String> object that generates display names based on the input value. The third parameter is a
ThrowingConsumer<? super T> object that contains the testing logic.
In the code below, the input values are integers from
10. The display names are generated using format
Test - %s. The actual test is to verify the numbers are greater than
The figure below shows the result of running this test factory in IntelliJ IDEA.
We can also use static method
DynamicContainer.dynamicContainer(String displayName, Stream<? extends DynamicNode> dynamicNodes) to create
DynamicContainers. In the code below, we create nested tests using
Check out the book JUnit 5 Cookbook for more new features in JUnit 5.