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
.
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
class NestedOrderTest {
@Nested
class NestedTest1 {
@Test
void testSuccess1() {
System.out.println(1);
}
}
@Nested
class NestedTest2 {
@Test
void testSuccess2() {
System.out.println(2);
}
}
@Nested
class NestedTest3 {
@Test
void testSuccess3() {
System.out.println(3);
}
}
}
Outputs:
3
2
1
By default, unit testing libraries don't try to execute tests in the order that occurs in the source file.
JUnit 5 as JUnit 4 work in that way. Why ? Because if the order matters it means that some tests are coupled between them and that is undesirable for unit tests (emphasis is mine) :
2.9. Test Execution Order
By default, test methods will be ordered using an algorithm that is deterministic but intentionally nonobvious. This ensures that subsequent runs of a test suite execute test methods in the same order, thereby allowing for repeatable builds.
So it is expected that the @Nested
feature introduced by JUnit 5 follows the same default approach.
But for integration tests, the order of the test method may matter since the state is not necessarily cleaned between the test methods executed.
So in JUnit 5 you have all the same the possibility to set the execution order by annotating the test class with @TestMethodOrder(OrderAnnotation.class)
and by specifying the order with @Order(numericOrderValue)
for the methods which the order matters.
But @Order
is designed to decorate methods to test, not the test class itself as you would like :
@API(status = EXPERIMENTAL, since = "5.4")
@Target({ ElementType.FIELD, ElementType.METHOD })
public @interface Order {...}
You can also note that that is an experimental feature.
So the single thing that you could order here are the test methods inside the same @Nested
scope, for example :
@Nested
class NestedTest1 {
@Order(3)
@Test
void success3() {
System.out.println(3);
}
@Order(2)
@Test
void success2() {
System.out.println(2);
}
@Order(1)
@Test
void success1() {
System.out.println(1);
}
}
Output :
1
2
3
By the way, specifying @TestMethodOrder(OrderAnnotation.class)
looks like not needed (at least in the 5.4.0 version I tested).