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:

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" }

		};

	}
}

TestNG Class:

Extend Data provider class in test class.

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);

	}

	
}

Output:

[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
===============================================

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.

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" }

		};

	}
}

OR

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" }

		};

	}
}

TestNG class:

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);

	}

	
}

Output:

[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
===============================================

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.

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" }

		};

	}
}

TestNG class:

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);

	}

	
}

Output:

[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
===============================================

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning