Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test expectedExceptionsMessageRegExp (exception message) using TestNG?

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?

like image 225
Isabel Jinson Avatar asked Dec 07 '25 15:12

Isabel Jinson


1 Answers

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)

like image 178
Cedric Beust Avatar answered Dec 10 '25 05:12

Cedric Beust



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!