JUnit 5 introduces dynamic tests, allowing test cases to be created at run time rather than compile time. Using `@TestFactory`, `DynamicTest`, and `DynamicContainer`, developers can generate parameterized tests from data collections, mirror hierarchical data structures in the test plan, and even define tests using lambda expressions. This addresses long-standing limitations of JUnit 4, where all tests had to be declared statically. The post covers practical patterns including streaming test data into dynamic tests, recursively building test trees for hierarchical data, and an experimental lambda-based test definition style using double brace initialization and reflection.

11m read timeFrom nipafx.dev
Post cover image
Table of contents
▚ Static Tests In JUnit 4▚ Dynamic Tests▚ Parameterized Tests▚ Hierarchical Tests▚ Lambda Tests▚ Reflection

Sort: