TestNG Tutorials 52: DataProvider in TestNG – Accessing DataProvider Methods From Another Class Test Class

Hello Guys,

Now we have good understanding of DataProviders in TestNG from previous articles. If you have not read my previous posts on DataProvider, you can go through them below:

Why do you need DataProvider?

How does DataProvider work?

DataProvider with 2D array

You can notice in all previous posts that I kept DataProvider methods and Test methods in same class. But it is good practice to keep Data and Test separated.  Following the same concept, we can keep DataProvider methods and Test in different classes. But it is little tricky which we are going to learn now.

There are two ways to do this:

  1. Inheritance
  2. Static or Non-static DataProvider Methods

Accessing DataProvider methods from other class to test class using Inheritance:

TestNG first looks for DataProvider method in same class and if it is not found then it looks in its base class if any. So if we inherit DataProvider class in TestNG class, it will find DataProvider methods from Base class. Example is as below:

DataProvider class:

define data provider method in a separate class as below:

[java]package DataProvider;

import org.testng.annotations.DataProvider;

public class DataProviderClass {

// A data provider method with return type as 2D array
@DataProvider(name = "DataContainer")
public Object[] myDataProvider() {
return new Object[][] {
{ "Mukesh", "Otwani", "Motwani@gmail.com" },
{ "Amod", "Mahajan", "Amahajan@hotmail.com" },
{ "Animesh", "Prashant", "aprashant@gmail.com" },
{ "Ankur", "Singh", "asingh@gmail.com" },
{ "Amritansh", "Kumar", "akumar@gmail.com" }

};

}
}
[/java]

TestNG Class:

Extend Data provider class in test class.

[java]package DataProvider;

import org.testng.annotations.Test;

// Extend DataProvider class
public class DataProviderBasics3 extends DataProviderClass{

// You must need to mention data provider method name in Test method
@Test(dataProvider = "DataContainer")
public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
System.out.println("Registered student with details: " + First_name + " " + Last_Name + " " + Email_ID);

}

}
[/java]

Output:

[xml][RemoteTestNG] detected TestNG version 6.14.2
Registered student with details: Mukesh Otwani Motwani@gmail.com
Registered student with details: Amod Mahajan Amahajan@hotmail.com
Registered student with details: Animesh Prashant aprashant@gmail.com
Registered student with details: Ankur Singh asingh@gmail.com
Registered student with details: Amritansh Kumar akumar@gmail.com
PASSED: studentRegistration("Mukesh", "Otwani", "Motwani@gmail.com")
PASSED: studentRegistration("Amod", "Mahajan", "Amahajan@hotmail.com")
PASSED: studentRegistration("Animesh", "Prashant", "aprashant@gmail.com")
PASSED: studentRegistration("Ankur", "Singh", "asingh@gmail.com")
PASSED: studentRegistration("Amritansh", "Kumar", "akumar@gmail.com")

===============================================
Default test
Tests run: 5, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

[/xml]

Making static/Non-static DataProvider methods to access it in TestNG class:

Since multiple inheritance is not possible in Java, you may need to inherit another class other than DataProvider class. You can use multilevel inheritance but again it will require extra coding and classes. You can create static or non-static DataProvider methods and can give it reference in TestNG class using “dataProviderClass” attribute in @Test method. Now question is when to declare with static or non-static?

If your DataProvider class has parameterised constructors, declare dataprovider methods as STATIC. If your DataProvider has default or no-argument constructor, you can declare data provide rmethods either static or non-static.

An example is below:

DataProvider class:

Below class has no constructor. So we can mark data provider method as static or non-static.

[java]package DataProvider;

import org.testng.annotations.DataProvider;

public class DataProviderClass {

// A data provider method with return type as 2D array
@DataProvider(name = "DataContainer")
public static Object[] myDataProvider() {
return new Object[][] {
{ "Mukesh", "Otwani", "Motwani@gmail.com" },
{ "Amod", "Mahajan", "Amahajan@hotmail.com" },
{ "Animesh", "Prashant", "aprashant@gmail.com" },
{ "Ankur", "Singh", "asingh@gmail.com" },
{ "Amritansh", "Kumar", "akumar@gmail.com" }

};

}
}
[/java]

OR

[java]package DataProvider;

import org.testng.annotations.DataProvider;

public class DataProviderClass {

// A data provider method with return type as 2D array
@DataProvider(name = "DataContainer")
public Object[][] myDataProvider() {
return new Object[][] {
{ "Mukesh", "Otwani", "Motwani@gmail.com" },
{ "Amod", "Mahajan", "Amahajan@hotmail.com" },
{ "Animesh", "Prashant", "aprashant@gmail.com" },
{ "Ankur", "Singh", "asingh@gmail.com" },
{ "Amritansh", "Kumar", "akumar@gmail.com" }

};

}
}
[/java]

TestNG class:

[java]package DataProvider;

import org.testng.annotations.Test;

// Extend DataProvider class
public class DataProviderBasics3{

// You must need to mention data provider method name in Test method
@Test(dataProvider = "DataContainer", dataProviderClass = DataProviderClass.class)
public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
System.out.println("Registered student with details: " + First_name + " " + Last_Name + " " + Email_ID);

}

}
[/java]

Output:

[xml][RemoteTestNG] detected TestNG version 6.14.2
Registered student with details: Mukesh Otwani Motwani@gmail.com
Registered student with details: Amod Mahajan Amahajan@hotmail.com
Registered student with details: Animesh Prashant aprashant@gmail.com
Registered student with details: Ankur Singh asingh@gmail.com
Registered student with details: Amritansh Kumar akumar@gmail.com
PASSED: studentRegistration("Mukesh", "Otwani", "Motwani@gmail.com")
PASSED: studentRegistration("Amod", "Mahajan", "Amahajan@hotmail.com")
PASSED: studentRegistration("Animesh", "Prashant", "aprashant@gmail.com")
PASSED: studentRegistration("Ankur", "Singh", "asingh@gmail.com")
PASSED: studentRegistration("Amritansh", "Kumar", "akumar@gmail.com")

===============================================
Default test
Tests run: 5, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

[/xml]

But if your data provider class has parameterised constructor you should declare Data Provider method as static.

Data Provider class:
This class has parameterised constructor in it.

[java]package DataProvider;

import org.testng.annotations.DataProvider;

public class DataProviderClass {

DataProviderClass(String s)
{
System.out.println("I am parameterised constructor so you can not have non-sttaic data"
+ "provider methods");
}

// A data provider method with return type as 2D array
@DataProvider(name = "DataContainer")
public static Object[][] myDataProvider() {
return new Object[][] {
{ "Mukesh", "Otwani", "Motwani@gmail.com" },
{ "Amod", "Mahajan", "Amahajan@hotmail.com" },
{ "Animesh", "Prashant", "aprashant@gmail.com" },
{ "Ankur", "Singh", "asingh@gmail.com" },
{ "Amritansh", "Kumar", "akumar@gmail.com" }

};

}
}
[/java]

TestNG class:

[java]package DataProvider;

import org.testng.annotations.Test;

// Extend DataProvider class
public class DataProviderBasics3{

// You must need to mention data provider method name in Test method
@Test(dataProvider = "DataContainer", dataProviderClass = DataProviderClass.class)
public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
System.out.println("Registered student with details: " + First_name + " " + Last_Name + " " + Email_ID);

}

}
[/java]

Output:

[xml][RemoteTestNG] detected TestNG version 6.14.2
Registered student with details: Mukesh Otwani Motwani@gmail.com
Registered student with details: Amod Mahajan Amahajan@hotmail.com
Registered student with details: Animesh Prashant aprashant@gmail.com
Registered student with details: Ankur Singh asingh@gmail.com
Registered student with details: Amritansh Kumar akumar@gmail.com
PASSED: studentRegistration("Mukesh", "Otwani", "Motwani@gmail.com")
PASSED: studentRegistration("Amod", "Mahajan", "Amahajan@hotmail.com")
PASSED: studentRegistration("Animesh", "Prashant", "aprashant@gmail.com")
PASSED: studentRegistration("Ankur", "Singh", "asingh@gmail.com")
PASSED: studentRegistration("Amritansh", "Kumar", "akumar@gmail.com")

===============================================
Default test
Tests run: 5, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================

[/xml]

It’s done. Hope you are clear with how can we access data provider method from other class.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

2 thoughts on “TestNG Tutorials 52: DataProvider in TestNG – Accessing DataProvider Methods From Another Class Test Class

  1. ” if your data provider class has parameterised constructor you should declare Data Provider method as static”
    Why dataprovider method has to be static if data provider class has a parameterised constructor. Call to dataprovider method does not depend on constructor. According to testng document all data provider methods should be stattic if caaled from a different class.

    1. It’s not mandatory to make data provider method static if used in different class. You need to make it static only if your data provider class has parameterised constructor in it. It must have some logic of creating object of Data provider class using default constructor to access data provider method. Since default parameter is abolished in case of parameterised constructor, we need to mark data provider method as static. You can try both examples. Thanks. Let me know if you have any doubt.

Leave a Reply to Amod Mahajan Cancel reply

Your email address will not be published. Required fields are marked *