Spring Junit Test 手动指定@Test运行顺序

在项目中写JunitTest类时,test的运行顺序默认是指定的,不变的,但是这个顺序是无法预估的。有时候我们想要手动去指定Test的运行顺序,在org.junit包下有个类@FixMethodOrder,这个注解类可以传入一个参数MethodSorters,具体的代码如下:

package org.junit.runners;

import java.lang.reflect.Method;
import java.util.Comparator;

import org.junit.internal.MethodSorter;

/**
 * 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;
    }
}

但是需要注意的是,这个值在junit 4.11后才有。

在gradle运行时,有可能不生效,可能是junit版本比较低。

  • MethodSorter.DEFAULT

    按照一个固定的顺序指定,但是这个顺序不可以预知。

  • MethodSorter.NAME_ASCENDING

    按照方法名字升续进行排序执行

  • MethodSorter.JVM

    jvm随机运行

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,936评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,742评论 18 399
  • JUnit Intro Android基于JUnit Framework来书写测试代码。JUnit是基于Java语...
    chandarlee阅读 2,303评论 0 50
  • 我想设计一场重逢 来见久别的你 快要永别的你 你只管来 我只管笑 我还是那副你爱或不爱的样子 你还是那副我一直爱着...
    你缺席的歲月阅读 427评论 0 0