语言 · 笔记

JUnit4 注解使用案例

JUnit Java Test

测试方法执行顺序

从 JUnit@4.11 版本开始 提供 @FixedMethodOrder 注解和 MethodSorters 类设置执行顺序。MethodSorters 有三个枚举值。

枚举值描述
MethodSorters.DEFAULT默认执行顺序,规则未知
MethodSorters.JVM每次执行都由 JVM 指定随机顺序执行
MethodSorters.NAME_ASCENDING按照方法名称的字典顺序对测试方法进行排序

示例:

java
@FixMethodOrder(MethodSorters.DEFAULT)
public class JUnit4TestOrder {
    @Test
    public void Testcase_3() {
        System.out.println("Testcase_3 executes");
    }

    @Test
    public void Testcase_1() {
        System.out.println("Testcase_1 executes");
    }

    @Test
    public void Testcase_2() {
    System.out.println("Testcase_2 executes ");
    }
}

源码:

java
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface FixMethodOrder {
    /**
      * Optionally specify <code>value</code> to have the methods executed in a particular order
      */
    MethodSorters value() default MethodSorters.DEFAULT;
}

不难看出默认采用MethodSorters.DEFAULT执行顺序