I am trying to use the TestNG framework to test values that should throw an exception. To do this I'm using a @DataProvider
to test different values at once rather than running multiple methods with different data values.
Essentially I'm expecting multiple and a variety of error messages depending on what failed ("data1 is too short"), (data1 is too long), ("data2 is too short"), (data2 is too long), etc For this example I will say I want data1 to be of length 5 and data2 of length 6
What I want to do is to test the error messages outputted as well. But when I try by using expectedExceptionsMessageRegExp
I can't seem to use multiple values with 'or' or commas.
So what I want to know is can I test for specific messages returned in console using the testNG @DataProvider
.
I already have it working without the DataProvider but it uses multiple methods and seems sloppy to me So with multiple expectedExceptions
types but can't with expectedExceptionsMessageRegExp
.
My Method:
@Test(dataProvider = "TestNGProblemsDP", expectedExceptions = {IllegalArgumentException.class},
expectedExceptionsMessageRegExp = ("data1 is too short"))
public void TestNGProblems(String data1, String data2) throws Exception {
DO STUFF WHERE DATA1 SHOULD BE LENGTH 5 AND DATA2 SHOULD BE LENGTH 6
}
The @DataProvider:
@DataProvider(name = "TestNGProblemsDP")
public Object[][] dataProvider(Method m) {
switch(m.getName()) {
case "TestNGProblems"
return new Object[][] {
{"hello", "world!"}, //pass
{"hell", "world!"}, //fail
{"hello", "orld!"}, //fail
{"hello", "word"}, //fail
{"", ""} //fail
case....
};
So what actually happened was I got a TestNG Exception if I didn't have the correct message:
org.testng.TestException: The exception was thrown with the wrong message: expected "data1 is too short" but got "data1 is too long"
If I tried to add multiple messages ie:
expectedExceptionsMessageRegExp = ("data1 is too short") || ("data1 is too long"))
or:
expectedExceptionsMessageRegExp = ("data1 is too short"), ("data1 is too long"))
It just wouldn't work.
Thank you to everyone who reads this and sorry for formatting etc, my first post :)
You can add one more field to the set of values delivered by your data provider where that new field would contain expected error message.
Then you just wrap your test in try-catch
and use that additional parameter to verify the actual message within the catch
block.