以下内容引自: https://howtodoinjava.com/testng/testng-expected-exception-and-expected-message-tutorial/
TestNG – Expected Exception and Expected Message Tutorial
While writing unit tests there can be certain scenarios where we need to verify that an exception is being thrown by the program during execution. TestNG provides a feature to test such scenarios by allowing the user to specify the type of exceptions that are expected to be thrown by a test methodduring execution. It supports multiple values being provided for verification. If the exception thrown by the test is not part of the user entered list, the test method will be marked as failed.
Let’s create a sample test and learn how exception test works in TestNG.
@Test ( expectedExceptions = { IOException.class, NullPointerException.class } )
Let’s see an example to understand it better.
Example of Expected Exception Test
In below test, we have two test methods i.e. exceptionTestOne()
and exceptionTestTwo()
. Here exceptionTestOne()
throws IOException
where as exceptionTestTwo()
throws Exception
. The expected exception to validate while running these tests is mentioned using the expectedExceptions attribute value while using the Test annotation.
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(); } } |
Output of above test run is given below:
[TestNG] Running: C:Userssomepath estng-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 =============================================== |
As you can see from the test results, exceptionTestTwo()
was marked as failed by TestNG during execution. The test failed because the exception thrown by the said method does not match the exception list provided in the expectedExceptions list.
Example of Expected Exception Test with Verifying Message
You can also verify a test based on the exception message that was thrown by the test. Regular expression can also be used to verify the error message, this can be done using .*.
Depending upon the position of the regular expression we can use it to do pattern matching such as starts-with, contains, and ends-with while verifying the exception message.
Let’s learn how to write a exception test based on the exception message thrown.
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" ); } } |
Output of above test run is given below:
[TestNG] Running: C:Userssomepath estng-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 =============================================== |
In above test methods exceptionTestThree()
failed because expected message didn’t matched.