How to use @DataProvider present in different class

Gaurav G Raj picture Gaurav G Raj · May 1, 2017 · Viewed 10.7k times · Source

How to use @DataProvider that is present in a different class?

I have created a different package and I have defined data providers next to each test cases. Please share how I may to use that in a different class.

Answer

juherr picture juherr · May 1, 2017

You can use the dataProviderClass attribute of @Test:

public class StaticProvider {
  @DataProvider(name = "create")
  public static Object[][] createData() {
    return new Object[][] {
      new Object[] { new Integer(42) }
    };
  }
}

public class MyTest {
  @Test(dataProvider = "create", dataProviderClass = StaticProvider.class)
  public void test(Integer n) {
    // ...
  }
}

Check the documentation for more details.