expectedExceptionsMessageRegExp正在尝试匹配detailMessage字段。我们可以匹配原因文本吗?即Exception.getCause()返回的文本? 这是因为detailMessage字段提供了非常通用的消息,如果预期的消息与该文本匹配,它将超越测试用例的目的。
@Test(expectedExceptions = TestExecutionException.class, expectedExceptionsMessageRegExp = ".* HTTP 422.*")
public void test() throws Exception {
..
//some code that produces TestExecutionException with the cause HTTP 422
..
}
TestNG错误是:
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
答案 0 :(得分:0)
TestNG依赖于Reflection来实例化您的测试类,然后调用@Test
方法。因此,@Test
方法的异常会触发java.lang.reflect.InvocationTargetException
getCause()
实际导致@Test
方法引发的异常。
TestNG旨在查询InvocationTargetException.getCause().getMessage()
以获取引发的异常的错误消息,然后尝试使用通过{{1} expectedExceptionsMessageRegExp
属性提供的正则表达式匹配它注释。
以下是使用TestNG @Test
6.12