expectedExceptionsMessageRegExp is trying to match the detailMessage field. Can we match the cause text? i.e the text returned by Exception.getCause() ? This is because the detailMessage field gives very generic message and it will beat the purpose of the test case if expected message is matched with that text.
@Test(expectedExceptions = TestExecutionException.class, expectedExceptionsMessageRegExp = ".* HTTP 422.*")
public void test() throws Exception {
..
//some code that produces TestExecutionException with the cause HTTP 422
..
}
The TestNG error is:
The exception was thrown with the wrong message: expected ".* HTTP 422.*" but got "Failed executing MessageExecutionTask"
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
TestNG relies on Reflection to instantiate your test class and then invoke the
@Testmethod. So an exception from the@Testmethod would trigger anjava.lang.reflect.InvocationTargetExceptionwhosegetCause()actually results in the exception raised by the@Testmethod.TestNG is designed to query the
InvocationTargetException.getCause().getMessage()to get the error message of the exception that was raised and then try and match it using the regex that is supplied via theexpectedExceptionsMessageRegExpattribute of the@Testannotation.Here's an example that runs fine with TestNG
6.12