Skip to content
Advertisement

JUnit5: Specify order of execution of multiple @Nested classes

Does Junit5 give any guarantees regarding execution order of @Nested classes defined within the same parent? With a trivial example I made, I notice the tests are executed in reverse order of declaration, but this behavior is neither documented (or is it?) nor consistent with how @Test annotated methods are ordered by default.

It’s also quite annoying, because obviously I’d rather have a non-reverse order, if not a way of configuring it akin to @TestMethodOrder.

JavaScript

Outputs:

JavaScript

Advertisement

Answer

The @Nested class ordering feature has been added in Junit 5.8. At this point, the platform gives all the guarantees users may need.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement