Spring Junit Test 手動(dòng)指定@Test運(yùn)行順序

在項(xiàng)目中寫(xiě)JunitTest類(lèi)時(shí),test的運(yùn)行順序默認(rèn)是指定的,不變的,但是這個(gè)順序是無(wú)法預(yù)估的。有時(shí)候我們想要手動(dòng)去指定Test的運(yùn)行順序,在org.junit包下有個(gè)類(lèi)@FixMethodOrder,這個(gè)注解類(lèi)可以傳入一個(gè)參數(shù)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;
    }
}

但是需要注意的是,這個(gè)值在junit 4.11后才有。

在gradle運(yùn)行時(shí),有可能不生效,可能是junit版本比較低。

  • MethodSorter.DEFAULT

    按照一個(gè)固定的順序指定,但是這個(gè)順序不可以預(yù)知。

  • MethodSorter.NAME_ASCENDING

    按照方法名字升續(xù)進(jìn)行排序執(zhí)行

  • MethodSorter.JVM

    jvm隨機(jī)運(yùn)行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,937評(píng)論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,958評(píng)論 6 342
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 31,765評(píng)論 18 399
  • JUnit Intro Android基于JUnit Framework來(lái)書(shū)寫(xiě)測(cè)試代碼。JUnit是基于Java語(yǔ)...
    chandarlee閱讀 2,305評(píng)論 0 50
  • 我想設(shè)計(jì)一場(chǎng)重逢 來(lái)見(jiàn)久別的你 快要永別的你 你只管來(lái) 我只管笑 我還是那副你愛(ài)或不愛(ài)的樣子 你還是那副我一直愛(ài)著...
    你缺席的歲月閱讀 428評(píng)論 0 0