The traditional coding model for tests in Java expects all tests to be compiled, and then executed at runtime. However, JUnit Jupiter introduces a paradigm for writing "dynamic tests", which are generated during runtime. The key annotations to this feature are
@TestFactory, which is used as an alternative to
@Test for methods that generate other test methods. A test factory must have a return type that includes the
DynamicTest class, such as a
Stream of these objects.
A dynamic test is created through a static method called
dynamicTest() inside the
DynamicTest class. It receives a string as its display name, as well as a lambda function, which describes the body of the generated test.
- Dynamic Tests are generated at runtime, and written using the
@TestFactorycan create dynamic nodes