Use the annotation @FixMethodOrder
with the method sorter MethodSorters.NAME_ASCENDING
. This will run all tests within the class in a deterministic and predictable order. The implementation compares the method names and in the case of a tie, it compares the methods' toString()
.
Code Segment Below Taken from JUnit Github -- MethodSorter.java
public int compare(Method m1, Method m2) {
final int comparison = m1.getName().compareTo(m2.getName());
if(comparison != 0) {
return comparison;
}
return m1.toString().compareTo(m2.toString());
}
Example
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedTest {
@Test
public void testA() {}
@Test
public void testB() {}
@Test
public void testC() {}
}
The execution order is