<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # JUnit 測試超時 – JUnit5 超時示例 > 原文: [https://howtodoinjava.com/junit/how-to-force-timeout-in-jnuit-testcase-execution/](https://howtodoinjava.com/junit/how-to-force-timeout-in-jnuit-testcase-execution/) 學習編寫具有超時行為的 JUnit 測試。 如果任何測試未在給定的時限內完成執行,則 JUnit 將停止執行。 還學習使用斷言使用 **JUnit5 測試超時**。 ## 1\. 為什么要編寫 JUnit 超時測試? 有時,我們必須編寫 JUnit 測試,以訪問網絡上的某些外部系統。 絕對不能 100% 地確定在執行測試用例時這些外部系統將可用。 這就是為什么在編寫具有外部依賴項的測試用例時,建議使用 JUnit 框架的`timeout`屬性。 這也被認為是 [**JUnit 最佳實踐**](//howtodoinjava.com/best-practices/unit-testing-best-practices-junit-reference-guide/ "Unit testing best practices : Junit Reference?guide") 。 > 每個 JUnit 測試都在一個新線程中運行。 如果在測試完成之前經過了指定的超時,則將通過`Thread.interrupt()`中斷其執行。 ## 2\. JUnit 測試超時示例 – `timeout`屬性 為了指定某個測試用例的超時周期,在注解`@Test`上提到“`timeout`”屬性。 例如,`@Test(timeout = 1000)`。 超時時間以**毫秒**指定。 ```java @Test(timeout = 500) public void testInfiniteTametakingLoop() throws InterruptedException { while (true) { Thread.currentThread().sleep(1000); } } ``` 在上述測試中,執行將在 5 秒后超時,并顯示以下消息。 ```java java.lang.Exception: test timed out after 5000 milliseconds ``` ## 2\. JUnit 全局超時示例 – 超時規則 可以為類中的所有測試定義 **JUnit 規則**,而不是為所有測試分別指定超時屬性。 ```java @Rule public Timeout globalTimeout = Timeout.seconds(2); @Test //Pass public void testInfiniteTametakingLoop1() throws InterruptedException { while (true) { Thread.currentThread().sleep(1000); } } @Test //Fail public void testInfiniteTametakingLoop2() throws InterruptedException { while (true) { Thread.currentThread().sleep(5000); } } ``` 在上面的示例中,第一個測試將通過,而第二個測試將失敗。 請注意,以上超時時間`@Rule`同樣適用于`@Before`和`@After`方法。 因此,請小心使用。 ## 4\. 使用斷言的 JUnit5 超時 在 [**JUnit5**](https://howtodoinjava.com/junit-5-tutorial/) 中,我們可以使用[斷言](https://howtodoinjava.com/junit5/junit-5-assertions-examples/)強制測試超時。 ```java import static java.time.Duration.ofMillis; import static java.time.Duration.ofMinutes; import static org.junit.jupiter.api.Assertions.assertTimeout; @Test void timeoutNotExceeded() { //The following assertion succeeds. assertTimeout(ofMinutes(2), () -> { // Perform task that takes less than 2 minutes. }); } @Test void timeoutExceeded() { // The following assertion fails with an error message similar to: // execution exceeded timeout of 10 ms by 91 ms assertTimeout(ofMillis(10), () -> { // Simulate task that takes more than 10 ms. Thread.sleep(100); }); } ``` 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看