如何使用 TestNG 测试预期异常消息RegExp(异常消息)?
我在 @Test
注释上使用 expectedExceptionsMessageRegExp
属性来测试异常消息,但它未正确执行。请参阅下面的代码。
单元测试代码:
@Test (dependsOnMethods = "test1", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "incorrect argument")
public void testConverter()
{
try
{
currencyConverter = Converter.convert(val1,val2)
}
catch (MYException e)
{
e.printStackTrace();
}
}
应用程序代码:
if (val1 == null || val1.length() == 0)
{
throw new IllegalArgumentException("Val1 is incorrect");
}
单元测试代码应检查异常消息,如果消息彼此不匹配,则测试应失败。
目前这还没有发生。我做错了什么吗?
I'm using the expectedExceptionsMessageRegExp
propery on the @Test
annotation to test an exception message, but it is not executing correctly. See the code below.
Unit test code:
@Test (dependsOnMethods = "test1", expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "incorrect argument")
public void testConverter()
{
try
{
currencyConverter = Converter.convert(val1,val2)
}
catch (MYException e)
{
e.printStackTrace();
}
}
Application code:
if (val1 == null || val1.length() == 0)
{
throw new IllegalArgumentException("Val1 is incorrect");
}
The unit test code should check the exception message, if the messages are not matching each other, the test should fail.
At present this is not happening. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎对我有用:
您可以发布一个简单的类来显示问题吗? (在这里或 testng-users 邮件列表上)
It seems to work for me:
Can you post a simple class showing the problem? (either here or on the testng-users mailing-list)