UnitTest Tips:固定用例顺序

JUnit默认不会控制用例的执行顺序,执行顺序由运行时查找到的用例列表的顺序决定。

如果有我们有一系列的用例,同时希望这些用例以一定的顺序执行,可以在类上添加注解:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

使JUnit以字母序列的方式执行用例。接着便是以一定的顺序,命名用例即可。

其他可选值

/**
 * 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;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,440评论 0 5
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,597评论 0 4
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,726评论 0 3
  • Android编码规范 源文件基础 文件名 源文件以其最顶层的类名来命名,大小写敏感,文件扩展名为.java。 文...
    呼呼哥阅读 986评论 0 0
  • 今天孕14周+6天,宝宝在妈妈肚子里第104天。 早上睡到九点半,妈妈赶紧叫爸爸起床,爸爸摸到十点...
    卢映妙阅读 212评论 0 3