Types of DynamicTest

Let's learn how to dynamically test simple and nested methods in JUnit 5.

There are two types of dynamic tests:

  • Simple DynamicTest
  • Nested DynamicTest

Simple DynamicTest

All test cases we’ve created so far are static. They’re defined at compile-timeA time period when the programming code is converted to the machine code. and can’t be changed at runtime. In JUnit 5, we can create dynamic tests that are generated at runtimeA time period when a program is running. by factory methodsIt is a method that solves the problem of creating product objects without specifying their concrete classes.. These methods are annotated with the org.junit.jupiter.api.TestFactory annotation.

The method annotated with @TestFactory must return a Stream, Collection, Iterable, or Iterator of org.junit.jupiter.api.DynamicNode objects. The DynamicNode is abstract. It has two subclasses:

  • The org.junit.jupiter.api.DynamicContainer subclass is the container of other DynamicNode objects.
  • The org.junit.jupiter.api.DynamicTest subclass is a generated test case.

Get hands-on with 1200+ tech skills courses.