Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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”, “[email protected]” }, { “Amod”, “Mahajan”, “[email protected]” }, { “Animesh”, “Prashant”, “[email protected]” }, { “Ankur”, “Singh”, “[email protected]” },

{ “Amritansh”, “Kumar”, “[email protected]” }

};

} }

[/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 [email protected] Registered student with details: Amod Mahajan [email protected] Registered student with details: Animesh Prashant [email protected] Registered student with details: Ankur Singh [email protected] Registered student with details: Amritansh Kumar [email protected] PASSED: studentRegistration(“Mukesh”, “Otwani”, “[email protected]”) PASSED: studentRegistration(“Amod”, “Mahajan”, “[email protected]”) PASSED: studentRegistration(“Animesh”, “Prashant”, “[email protected]”) PASSED: studentRegistration(“Ankur”, “Singh”, “[email protected]”)

PASSED: studentRegistration(“Amritansh”, “Kumar”, “[email protected]”)

=============================================== 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”, “[email protected]” }, { “Amod”, “Mahajan”, “[email protected]” }, { “Animesh”, “Prashant”, “[email protected]” }, { “Ankur”, “Singh”, “[email protected]” },

{ “Amritansh”, “Kumar”, “[email protected]” }

};

} }

[/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”, “[email protected]” }, { “Amod”, “Mahajan”, “[email protected]” }, { “Animesh”, “Prashant”, “[email protected]” }, { “Ankur”, “Singh”, “[email protected]” },

{ “Amritansh”, “Kumar”, “[email protected]” }

};

} }

[/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 [email protected] Registered student with details: Amod Mahajan [email protected] Registered student with details: Animesh Prashant [email protected] Registered student with details: Ankur Singh [email protected] Registered student with details: Amritansh Kumar [email protected] PASSED: studentRegistration(“Mukesh”, “Otwani”, “[email protected]”) PASSED: studentRegistration(“Amod”, “Mahajan”, “[email protected]”) PASSED: studentRegistration(“Animesh”, “Prashant”, “[email protected]”) PASSED: studentRegistration(“Ankur”, “Singh”, “[email protected]”)

PASSED: studentRegistration(“Amritansh”, “Kumar”, “[email protected]”)

=============================================== 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”, “[email protected]” }, { “Amod”, “Mahajan”, “[email protected]” }, { “Animesh”, “Prashant”, “[email protected]” }, { “Ankur”, “Singh”, “[email protected]” },

{ “Amritansh”, “Kumar”, “[email protected]” }

};

} }

[/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 [email protected] Registered student with details: Amod Mahajan [email protected] Registered student with details: Animesh Prashant [email protected] Registered student with details: Ankur Singh [email protected] Registered student with details: Amritansh Kumar [email protected] PASSED: studentRegistration(“Mukesh”, “Otwani”, “[email protected]”) PASSED: studentRegistration(“Amod”, “Mahajan”, “[email protected]”) PASSED: studentRegistration(“Animesh”, “Prashant”, “[email protected]”) PASSED: studentRegistration(“Ankur”, “Singh”, “[email protected]”)

PASSED: studentRegistration(“Amritansh”, “Kumar”, “[email protected]”)

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

Uncategorized

Post navigation

Previous Post: LeetCode – 100 Programs
Next Post: string

Related Posts

TestNG Tutorials 52: DataProvider in TestNG – Accessing DataProvider Methods From Another Class Test Class | Make Selenium Easy Uncategorized
January 7, 2018 – Make Selenium Easy Uncategorized
June 9, 2017 – Make Selenium Easy Uncategorized
Skipped1 – Make Selenium Easy Uncategorized
Frequently Asked Java Program 02: Java Program to Check if Any String is Palindrome Without Using Reverse Method | Make Selenium Easy Uncategorized
getAttribute() method in Selenium WebDriver – Why, What and How to use? Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark