1

// This is from another file but same package.

public class EmpData 
{
    @DataProvider
    public Object[][] getData()
    {
        Object[][] data= {{"Hello","text","1"},{"bye","Message","32"},{"solo","call","678"}}; 
        return data;
    }
}

// Type -1 ( This is from another file but the same package.)

public class DPClass extends EmpData  
{
    @Test(dataProvider="getData")
    public void testCaseData(String Greeting, String Communication, String Id)
    {
        System.out.println(Greeting+Communication+Id);
    }
}

// Type -2

public class DPClass  
{
    @Test(dataProvider="getData", dataProviderClass=DPClass.class)
    public void testCaseData(String Greeting, String Communication, String Id)
    {
        System.out.println(Greeting+Communication+Id);
    }
}

In above there are two separate file DPClass.java and EmpData.java in one single package we can provide data to DPClass in both way either add parameter in @Test annotation and by extending class with adding parameter in @Test annotation. If we are achieving this by extending class then why TestNG provided DataProviderClass? Correct me if I wrong for this concept, I need difference for both.

Gautham M
  • 4,816
  • 3
  • 15
  • 37

1 Answers1

1

That totally depends on how user needs to use it. If dataProviderClass option is not provided and if the user wants to keep data methods separate from the test methods, then the user is forced to extend the class as in your example.

public class DPClass {
    @DataProvider
    public Object[][] getData()
    {
        Object[][] data= {{"Hello","text","1"},{"bye","Message","32"},{"solo","call","678"}}; 
        return data;
    }
}

public class TestClass extends DPClass  
{
    @Test(dataProvider="getData")
    public void testCaseData(String Greeting, String Communication, String Id)
    {
        System.out.println(Greeting+Communication+Id);
    }
}

But imagine if your test class is already extending another class. So your test class cannot extend the DPClass (Classes in Java support single inheritance) and hence you can't keep data provider methods in a different class.

Example: For spring boot tests, if the test class requires spring application context, then the test class should extend AbstractTestNGSpringContextTests. So you cannot extend any more class.

Gautham M
  • 4,816
  • 3
  • 15
  • 37