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

2017-02-08  本文已影响0人  LoWang

在项目中写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版本比较低。

上一篇下一篇

猜你喜欢

热点阅读