<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # TestNG – 預期異常和預期消息教程 > 原文: [https://howtodoinjava.com/testng/testng-expected-exception-and-expected-message-tutorial/](https://howtodoinjava.com/testng/testng-expected-exception-and-expected-message-tutorial/) 在編寫單元測試時,在某些情況下,我們需要驗證在執行過程中程序是否引發了異常。 通過允許用戶**指定在執行過程中測試方法**引發的異常類型,TestNG 提供了一種測試此類情況的功能。 它支持為驗證提供多個值。 如果測試引發的異常不屬于用戶輸入列表,則測試方法將標記為失敗。 讓我們創建一個示例測試,并了解異常測試如何在 TestNG 中工作。 ```java @Test ( expectedExceptions = { IOException.class, NullPointerException.class } ) ``` 讓我們看一個例子,以更好地理解它。 ## 預期異常測試示例 在下面的測試中,我們有兩種測試方法,即`exceptionTestOne()`和`exceptionTestTwo()`。 這里`exceptionTestOne()`拋出`IOException`,而`exceptionTestTwo()`拋出`Exception`。 在使用`Test`注解時,使用`ExpectedExceptions`屬性值提到了在運行這些測試時驗證的預期異常。 ```java public class ExceptionTestDemo { @Test(expectedExceptions = { IOException.class }) public void exceptionTestOne() throws Exception { throw new IOException(); } @Test(expectedExceptions = { IOException.class, NullPointerException.class }) public void exceptionTestTwo() throws Exception { throw new Exception(); } } ``` 以上測試運行的輸出如下: ```java [TestNG] Running: C:\Users\somepath\testng-customsuite.xml PASSED: exceptionTestOne FAILED: exceptionTestTwo org.testng.TestException: Expected exception java.io.IOException but got org.testng.TestException: Expected exception java.io.IOException but got java.lang.Exception at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) Caused by: org.testng.TestException: Expected exception java.io.IOException but got java.lang.Exception at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497) at org.testng.internal.Invoker.invokeMethod(Invoker.java:754) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) ... 16 more Caused by: java.lang.Exception at com.howtodoinjava.test.ExceptionTestDemo.exceptionTestTwo(ExceptionTestDemo.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) ... 18 more =============================================== Default test Tests run: 2, Failures: 1, Skips: 0 =============================================== ``` 從測試結果中可以看出,`exceptionTestTwo()`在執行期間被 TestNG 標記為失敗。 測試失敗,因為所述方法引發的異常與`ExpectedExceptions`列表中提供的異常列表不匹配。 ## 帶有驗證消息的預期異常測試的示例 您還可以根據測試引發的異常消息來驗證測試。 正則表達式也可以用于驗證錯誤消息,可以使用`.*.`完成此操作,具體取決于正則表達式的位置,我們可以在驗證異常消息時使用它進行模式匹配,例如開始,包含,結束于。 讓我們學習如何根據拋出的異常消息編寫異常測試。 ```java public class ExceptionTestDemo { @Test(expectedExceptions = { IOException.class }, expectedExceptionsMessageRegExp = "Pass Message test") public void exceptionTestOne() throws Exception { throw new IOException("Pass Message test"); } @Test(expectedExceptions = { IOException.class }, expectedExceptionsMessageRegExp = ".* Message .*") public void exceptionTestTwo() throws Exception { throw new IOException("Pass Message test"); } @Test(expectedExceptions = { IOException.class }, expectedExceptionsMessageRegExp = "Pass Message test") public void exceptionTestThree() throws Exception { throw new IOException("Fail Message test"); } } ``` 以上測試運行的輸出如下: ```java [TestNG] Running: C:\Users\somepath\testng-customsuite.xml PASSED: exceptionTestOne PASSED: exceptionTestTwo FAILED: exceptionTestThree org.testng.TestException: Expected exception java.io.IOException but got org.testng.TestException: The exception was thrown with the wrong message: expected "Pass Message test" but got "Fail Message test" at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) Caused by: org.testng.TestException: The exception was thrown with the wrong message: expected "Pass Message test" but got "Fail Message test" at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1481) at org.testng.internal.Invoker.invokeMethod(Invoker.java:754) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) ... 16 more Caused by: java.io.IOException: Fail Message test at com.howtodoinjava.test.ExceptionTestDemo.exceptionTestThree(ExceptionTestDemo.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) ... 18 more =============================================== Default test Tests run: 3, Failures: 1, Skips: 0 =============================================== ``` 在上述測試方法中,`exceptionTestThree()`失敗,因為預期的消息不匹配。 **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看