I want to execute test methods which are annotated by @Test
in specific order.
For example:
public class MyTest {
@Test public void
JUnit since 5.5 allows @TestMethodOrder(OrderAnnotation.class)
on class and @Order(1)
on test-methods.
JUnit old versions allow test methods run ordering using class annotations:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@FixMethodOrder(MethodSorters.JVM)
@FixMethodOrder(MethodSorters.DEFAULT)
By default test methods are run in alphabetical order. So, to set specific methods order you can name them like:
a_TestWorkUnit_WithCertainState_ShouldDoSomething b_TestWorkUnit_WithCertainState_ShouldDoSomething c_TestWorkUnit_WithCertainState_ShouldDoSomething
Or
_1_TestWorkUnit_WithCertainState_ShouldDoSomething _2_TestWorkUnit_WithCertainState_ShouldDoSomething _3_TestWorkUnit_WithCertainState_ShouldDoSomething
You can find examples here.