Friday, 20 February 2015

How to pass class Object through Dataprovider TestNG

A DataProvider in TestNG is a method in a test class, which provides an array of varied actual values to dependent test methods.(WIKI)

Check below how to pass class object to test Method using DataProvider.

Please note in Test method, passed parameter type should be of type of Object that you have added to your object array in dataprovider method.

 @DataProvider(name = "dataForTest")
       public static Object[][]  data() 
       Object[][] data = new Object[1][1];
        Dummy dum = new Dummy("DummyName ", 246);
        return new Object []{dum};

public class testDataProvider {
    @Test(dataProviderClass = DataProvider.class,dataProvider = "dataForTest")
    public void testMethod(Dummy Dumb) {
        System.out.println("Dumb Name" +"  " +  Dumb.getName() +"   " + " Dumb Salary " + Dumb.getSalary());

Here is my Dumb class

public class Dummy {
    private String name;
    private int salary;
    public Dummy(String name, int salary){;
    public String getName() {
return name;
      public int getSalary() {
return salary;

One must Remember Point about test iterations

Object[][] data = new Object[1][1];
With above data ,your test will run 1 time and will take one parameter

Object[][] data = new Object[2][1];
and this one will run 2 times and will take one parameter.

No comments:

Post a Comment