Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set expectedExceptions using dataprovider

Is it possible to set the expectedExceptions option in a TestNG @Test from a @DataProvider? If so, how is it done?

Here is a non-intuitive example:

@DataProvider
private Object[][] methodABadArgsProvider() {
    return new Object[][] {
      {null, "arg2", "arg3"}, // expect NullPointerException
      {"arg1", null, "arg3"}, // expect IllegalArgumentException
      {"arg1", "arg2", null} // expect OperationNotSupportedException
    }
}

@Test(expectedException = [tbd].class, dataProvider = "methodABadArgsProvider")
public void methodABadArgs(String arg1, String arg2, String arg3) {
    testInstance.methodA(arg1, arg2, arg3);
}
like image 838
ishan3243 Avatar asked Aug 17 '26 06:08

ishan3243


1 Answers

DataProvider has access to test context. It's not possible to it via dp; but you can use AnnotationTransformer if it allows to do what you need.

like image 170
user1058106 Avatar answered Aug 18 '26 20:08

user1058106