<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # JUnit 有序測試執行示例 > 原文: [https://howtodoinjava.com/junit/ordered-testcases-execution-in-junit-4/](https://howtodoinjava.com/junit/ordered-testcases-execution-in-junit-4/) 編寫 **JUnit 有序測試**案例被認為是[不良做法](https://howtodoinjava.com/best-practices/unit-testing-best-practices-junit-reference-guide/)。 但是,如果仍然遇到測試用例排序是唯一出路的情況,則可以使用`MethodSorters`類。 ## 1\. JUnit 方法排序器 從 JUnit4.11 版本開始引入`MethodSorters`。 此類聲明了三種執行順序,可以在測試用例中使用它們。 1. **`MethodSorters.DEFAULT`** – 以確定但不可預測的順序對測試方法進行排序。 2. **`MethodSorters.JVM`** - 按照 JVM 返回的順序保留測試方法。 3. **`MethodSorters.NAME_ASCENDING`** – **按方法名稱**依字典順序對測試方法進行排序,并使用`Method.toString()`作為平局。 ## 2\. JUnit 有序測試示例 – `NAME_ASCENDING` 讓我們看看如何在 JUnit 中編寫和執行有序的測試。 ```java import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; //Running test cases in order of method names in ascending order @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class OrderedTestCasesExecution { @Test public void secondTest() { System.out.println("Executing second test"); } @Test public void firstTest() { System.out.println("Executing first test"); } @Test public void thirdTest() { System.out.println("Executing third test"); } } ``` 程序輸出。 ```java Executing first test Executing second test Executing third test ``` ## 2\. JUnit 有序測試示例 – JVM 現在使用`JVM`選項執行相同的測試。 ```java package corejava.test.junit; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.MethodSorters; //Running test cases in order of method names in ascending order @FixMethodOrder(MethodSorters.JVM) public class OrderedTestCasesExecution { @Test public void secondTest() { System.out.println("Executing second test"); } @Test public void firstTest() { System.out.println("Executing first test"); } @Test public void thirdTest() { System.out.println("Executing third test"); } } ``` 程序輸出: ```java Executing third test Executing first test Executing second test ``` 顯然,只有`NAME_ASCENDING`順序可讓您控制真正的順序,而其他兩個選項在**測試執行順序序列**中對于開發人員而言卻無法提供足夠的可預測性。 在此 [JUnit 教程](https://howtodoinjava.com/junit-5-tutorial/)中,我們學習了編寫 **JUnit 順序測試**的過程。 讓我知道你的想法。 學習愉快! 參考: [`MethodSorters`類](https://junit.org/junit4/javadoc/4.12/org/junit/runners/MethodSorters.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看