With TestNG, 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 functionality to test such exception scenarios by allowing the user to specify the type of exceptions that are expected to be thrown by a test during execution.
1. The expectedExceptions
Attribute
Within @Test
annotation, TestNG supports multiple exceptions being provided for verification using attribute expectedExceptions. If the exception thrown by the test is not part of the user entered list of exceptions, the test will be marked as failed.
@Test ( expectedExceptions = { IOException.class, NullPointerException.class } )
Let’s create an example test and learn how the exception testing works in TestNG.
2. Expected Exception Demo
In the 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 }) //Passes
public void exceptionTestOne() throws Exception {
throw new IOException();
}
@Test(expectedExceptions = { IOException.class, NullPointerException.class }) //Fails
public void exceptionTestTwo() throws Exception {
throw new Exception();
}
}
The output of above test run is given below:
[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
===============================================
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.
3. Asserting Execution Messages
We can also verify a test based on the exception message that was thrown by the test. Regular expressions can also be used to verify the error message.
Assertion of exception messages can also 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 an 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"); //Fails
}
}
The output of above test run is given below:
[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)
===============================================
Default test
Tests run: 3, Failures: 1, Skips: 0
===============================================
In above test methods exceptionTestThree()
failed because the expected message didn’t match.
Happy Learning !!
Leave a Reply