场景
一般跑单侧是不需要管顺序的,但是有时候写增删改查的时候,需要将新建的单侧先执行,然后在进行其他操作,最后删除,这时候,可以通过注解和测试的名称来调整单侧的执行顺序;
使用
Junit测试可以通过注解@FixMethodOrder调整测试顺序:
参数
也就是MethodSorters枚举的参数
- MethodSorters.JVM:按照JVM得到的方法顺序(每次都不一样)
- MethodSorters.DEFAULT:默认的顺序,以不可预期的顺序执行(不管你的单侧顺序,每次都是一样的)
- MethodSorters.NAME_ASCENDING:按照方法名字母顺序执行(每次一样)
/**
* Sort the methods into a specified execution order.
* Defines common {@link MethodSorter} implementations.
*
* @since 4.11
*/
public enum MethodSorters {
/**
* Sorts the test methods by the method name, in lexicographic order,
* with {@link Method#toString()} used as a tiebreaker
*/
NAME_ASCENDING(MethodSorter.NAME_ASCENDING),
/**
* Leaves the test methods in the order returned by the JVM.
* Note that the order from the JVM may vary from run to run
*/
JVM(null),
/**
* Sorts the test methods in a deterministic, but not predictable, order
*/
DEFAULT(MethodSorter.DEFAULT);
private final Comparator<Method> comparator;
private MethodSorters(Comparator<Method> comparator) {
this.comparator = comparator;
}
public Comparator<Method> getComparator() {
return comparator;
}
}
测试
@FixMethodOrder(MethodSorters.JVM)
public class MethodOrderTest {
@Test
public void btest() {
System.out.print("3");
}
@Test
public void atest2() {
System.out.print("1");
}
@Test
public void atest1() {
System.out.print("2");
}
@Test
public void ctest() {
System.out.print("4");
}
}