如何使用 TestNG 测试预期异常消息RegExp(异常消息)?

发布于 2024-08-29 01:33:07 字数 719 浏览 1 评论 0原文

我在 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2024-09-05 01:33:07

这似乎对我有用:

org.testng.TestException:
抛出异常并显示错误消息:预期“参数不正确”,但得到“val1 不正确”

您可以发布一个简单的类来显示问题吗? (在这里或 testng-users 邮件列表上)

It seems to work for me:

org.testng.TestException:
The exception was thrown with the wrong message: expected "incorrect argument" but got "val1 is incorrect"

Can you post a simple class showing the problem? (either here or on the testng-users mailing-list)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文