【问题标题】:How do I test expectedExceptionsMessageRegExp (exception message) using TestNG?如何使用 TestNG 测试 expectedExceptionsMessageRegExp(异常消息)?
【发布时间】:2010-04-14 15:25:39
【问题描述】:

我在@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");
}

单元测试代码应该检查异常消息,如果消息不匹配,测试应该失败。

目前这还没有发生。我做错了吗?

【问题讨论】:

    标签: java unit-testing testing testng


    【解决方案1】:

    这似乎对我有用:

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

    你能发布一个简单的类来显示这个问题吗? (在此处或在 testng-users 邮件列表中)

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2017-03-25
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 2011-08-20
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多