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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # JUnit5 預期的異常 – `assertThrows()`示例 > 原文: [https://howtodoinjava.com/junit5/expected-exception-example/](https://howtodoinjava.com/junit5/expected-exception-example/) 在 JUnit5 中,要*測試異常情況*,則應使用[`org.junit.jupiter.api.Assertions.assertThrows()`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assertions.html#assertThrows-java.lang.Class-org.junit.jupiter.api.function.Executable-)方法。 *JUnit5 異常測試*還有其他方法,但我建議避免使用它們。 ## 1\. JUnit5 `assertThrows()`的語法 它斷言所提供的`executable`的執行將引發`expectedType`的異常并返回該異常。 ```java public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable) ``` **如果沒有引發異常,或者引發了其他類型的異常,則此方法將失敗。** 請注意,允許使用相同類型的異常。 例如,如果您期望`NumberFormatException`并且拋出`IllegalArgumentException`,那么該測試也會通過,因為`NumberFormatException`擴展了`IllegalArgumentException`類。 ## 2\. JUnit5 預期異常示例 一個非常簡單的示例可以是: ```java @Test void testExpectedException() { Assertions.assertThrows(NumberFormatException.class, () -> { Integer.parseInt("One"); }); } ``` 如果參數不是有效數字,則此處的可執行代碼為`Integer.parseInt("One")`引發`NumberFormatException`。 在上述代碼中,`"One"`不是有效數字,因此代碼將引發`assertThrows()`方法所期望的異常 - 因此測試通過。 ## 3\. 預期異常類的超類型 如前所述,您也可以期望異常類的超類型。 例如,上面的測試也可以用`IllegalArgumentException`編寫。 ```java @Test void testExpectedExceptionWithSuperType() { Assertions.assertThrows(IllegalArgumentException.class, () -> { Integer.parseInt("One"); }); } ``` 該測試用例也將通過。 ## 4\. 其他預期異常類 如果可執行代碼**拋出任何其他異常類型**,則測試將失敗;否則,測試將失敗。 甚至**如果測試沒有引發任何異常**,那么測試也會失敗。 例如,在下面的示例中,`"1"`是有效數字,因此不會引發異常。 ```java @Test void testExpectedExceptionFail() { Assertions.assertThrows(IllegalArgumentException.class, () -> { Integer.parseInt("1"); }); } ``` ## 5\. 完整的例子 這是 *JUnit5 斷言異常消息*的完整代碼。 ```java package com.howtodoinjava.junit5.examples.module; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class AppTest { @Test void testExpectedException() { Assertions.assertThrows(NumberFormatException.class, () -> { Integer.parseInt("One"); }); } @Test void testExpectedExceptionWithSuperType() { Assertions.assertThrows(IllegalArgumentException.class, () -> { Integer.parseInt("One"); }); } @Test void testExpectedExceptionFail() { Assertions.assertThrows(IllegalArgumentException.class, () -> { Integer.parseInt("1"); }); } } ``` 現在,使用[測試套件](//howtodoinjava.com/junit-5/junit5-test-suites-examples/)執行此類。 您可以在 [maven 示例](//howtodoinjava.com/junit-5/junit-5-maven-dependency-pom-xml-example/)中查看完整的項目配置。 ```java package com.howtodoinjava.junit5.examples; import org.junit.platform.runner.JUnitPlatform; import org.junit.platform.suite.api.SelectPackages; import org.junit.runner.RunWith; @RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") public class JUnit5Example { } ``` Eclipse 測試結果如下圖所示: ![JUnit5 Expected Exception Tests](https://img.kancloud.cn/01/16/0116fbda9e845d77040eda1f51ac445c_573x211.png) JUnit5 預期異常測試 這就是 **JUnit5 異常測試**的全部。 將我的問題放在評論部分。 學習愉快! [源碼下載](https://github.com/lokeshgupta1981/Junit5Examples/tree/master/JUnit5Examples)
                  <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>

                              哎呀哎呀视频在线观看