How to check testExceptionsMessageRegExp (exception message) using TestNG?

I use the property expectedExceptionsMessageRegExpin the annotation @Testto check for an exception message, but it does not execute correctly. See 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 do not match, the test should fail.

This is currently not happening. Am I doing something wrong?

+3
source share
1 answer

This seems to work for me:

org.testng.TestException: : " ", "val1 is wrong"

, ? ( , testng-users)

+5

Source: https://habr.com/ru/post/1741107/


All Articles