The annotation used with the custom
OrderedRunner
JUnit runner. This allows to control the
ordering of tests in a test class.
For Java ≤ 6.0, the Oracle JVM execute the tests methods in the test class in the order they are declared
in the source. It is not the case anymore for Java ≥ 7.0, in this case it is not possible to rely on the
JVM for the execution in the test methods, creating specific test failures on Java 7 which would work
well for Java 6. The solution is to use a custom Test Runner where the ordering of test methods is defined
through an annotation.
In many cases, the ordering of tests cases is not imporetant, but in some specific
cases the state at the start of the next test case depends on the state at the end of the previous one.
The OrderedRunner class is a Test runner which will order the test methods according to the
value of the @Order annotation.
Example
If a test class has the two following methods:
public class MyTest {
@Test
public void test1() {
...
}
@Test
public void test2() {
...
}
nothing prevents the tests to be executed in the order test2()
then test1(). To ensure that
the order will be test1()
then test2(), you can use:
@RunWith(OrderedRunner.class)
public class MyTest {
@Test
@Order(order=1)
public void test1() {
...
}
@Test
@Order(order=2)
public void test2() {
...
}