3

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);
}
Barett
  • 5,826
  • 6
  • 51
  • 55
ishan3243
  • 1,870
  • 4
  • 30
  • 49

1 Answers1

1

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.

user1058106
  • 530
  • 3
  • 12