Importance: ★★ ★★ ★★

A Test method annotated @test is static because its behavior is completely determined at compile time and cannot be modified at run time. The Assumptions in the Assumptions provide a level of dynamic behavior that dynamically adjusts test execution based on runtime environment conditions.

JUnit Jupiter provides a new testing programming model. Dynamic tests can be generated at run time through TestFactory methods annotated with @testfactory.

Technically speaking, @ TestFactory test factory method must return a single DynamicNode instance, or composed of DynamicNode Stream, Collection, Iterable, the Iterator or array. DynamicNode has two instantiable subclasses:

DynamicContainer and DynamicTest. DynamicContainer includes a display name and a set of dynamic child nodes that can be used to create a nested dynamic node tree of any level. DynamicTest instances are deferred, enabling dynamic and even nondeterministic test case generation.

Any streams returned by @testFactory will be normally closed by JUnit by calling stream.close (), so it is safe to use resources like files.lines ().

Like the @test method, the @TestFactory method must be non-private and non-static, and can contain parameters that can be parsed by the parameter parser.

DynamicTest is a test case generated at run time by the test factory method that consists of a display name and an Executable.

Unlike @Test, the lifecycle method is for the entire @TestFactory Test factory method, not for each Test case generated by the Test factory method. That is, multiple test cases generated by the template method share the same test class instance, and a test case that changes the state of the test class can affect the next test case and must be treated with care.

Detailed dynamic test code demo

Original | TDD toolkit: JUnit, AssertJ and Mockito (21) write test – dynamic testing

The open source code of this article is downloaded here ↓↓↓

Code download | TDD tool set original open source code, free download!

If you feel there is a harvest, click a [praise] to encourage!