java - Can we use TestNG expectedExceptionsMessageRegExp to match the cause text? -
expectedexceptionsmessageregexp trying match detailmessage field. can match cause text? i.e text returned exception.getcause() ? because detailmessage field gives generic message , beat purpose of test case if expected message matched text.
@test(expectedexceptions = testexecutionexception.class, expectedexceptionsmessageregexp = ".* http 422.*") public void test() throws exception { .. //some code produces testexecutionexception cause http 422 .. }
the testng error is:
the exception thrown wrong message: expected ".* http 422.*" got "failed executing messageexecutiontask" @ org.testng.internal.invoker.handleinvocationresults(invoker.java:1481) @ org.testng.internal.invoker.invokemethod(invoker.java:754) @ org.testng.internal.invoker.invoketestmethod(invoker.java:901) @ org.testng.internal.invoker.invoketestmethods(invoker.java:1231) ... 16 more
testng relies on reflection instantiate test class , invoke @test
method. exception @test
method trigger java.lang.reflect.invocationtargetexception
getcause()
results in exception raised @test
method.
testng designed query invocationtargetexception.getcause().getmessage()
error message of exception raised , try , match using regex supplied via expectedexceptionsmessageregexp
attribute of @test
annotation.
here's example runs fine testng 6.12
import org.testng.itestresult; import org.testng.annotations.aftermethod; import org.testng.annotations.test; public class testclass { @test(expectedexceptions = oldmonkexception.class, expectedexceptionsmessageregexp = ".* http 422.*") public void test() throws exception { throw new oldmonkexception("your have triggered http 422 error code."); } @aftermethod public void aftertestmethod(itestresult testresult) { string mname = testresult.getmethod().getmethodname() + " "; switch (testresult.getstatus()) { case itestresult.success: mname += "passed"; break; case itestresult.failure: mname += "failed"; break; case itestresult.skip: mname += "skipped"; break; default: mname += ""; } system.err.println(mname); } public static class oldmonkexception extends exception { oldmonkexception(string message) { super(message); } } }
Comments
Post a Comment