Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 56: DataProvider in TestNG – Parameterizing DataProvider Method to Provide Data to Multiple Test Methods

Posted on 02/19/2025 By admin

“DataProvider” is an important feature provided by TestNG to provide required test data to test methods to run on. We have learnt different approaches which we can utilize to get best of DataProvider in previous posts.

Let’s learn another way to make more use of DataProvider in TestNG.

Suppose we have three Test methods and each test method requires different data sets to execute. The simplest way to achieve this is by creating a separate DataProvider method for each Test method which means you require three DataProvider method for 3 Test methods. But it will become complex to manage when we have a big count of Test methods.

Solution is Parameterized DataProvider method.

We can create parameterized  DataProvider method in such a way that it will return required data set as per Test method name. Let’s learn how.

We can pass a parameter of type Method which will be the actually currently running Test method and based on name of Test method we can decide what data need to be retuned. We can use switch concept to accommodate more number of test methods. DataProvider method with return type as an Object[] will help us to return different set of data as well. You can use Iterator return type as well. Based on your requirement you can decide it.

As per TestNG official document:

If you declare your @DataProvider as taking a java.lang.reflect.Method as first parameter, TestNG will pass the current test method for this first parameter. This is particularly useful when several test methods use the same @DataProvider and you want it to return different values depending on which test method it is supplying data for.

Example Program:

package DataProvider;

import java.lang.reflect.Method;


import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

// Data class
class India {
        public String getCapital() {
                return "New Delhi";
        }
}

class USA {
        public String getCapital() {
                return "New York";
        }
}

class UK {
        public String getCapital() {
                return "London";
        }
}

public class CustomDataProvider {

        // A data provider method which will return a required data based on Test method
        // name
        @DataProvider(name = "DataContainer")
        public static Object[] myDataProvider(Method m) {
                
                // Matching test method name
                // You can use switch as well.
                if (m.getName().equals("India")) {
                        India i[] = new India[1];
                        i[0] = new India();
                        return i;
                } else if (m.getName().equals("USA")) {
                        USA i[] = new USA[1];
                        i[0] = new USA();
                        return i;
                } else if (m.getName().equals("UK")) {
                        UK i[] = new UK[1];
                        i[0] = new UK();
                        return i;
                }
                else
                {
                        System.out.println("No data found");
                        //System.exit(0);
                        return null;
                }

        }

        // A test method
        @Test(dataProvider = "DataContainer")
        public void India(India capitalName) {
                System.out.println("Capital of India is " + capitalName.getCapital());
        }

        // A test method
        @Test(dataProvider = "DataContainer")
        public void USA(USA capitalName) {
                System.out.println("Capital of USA is " + capitalName.getCapital());
        }

        // A test method
        @Test(dataProvider = "DataContainer")
        public void UK(UK capitalName) {
                System.out.println("Capital of UK is " + capitalName.getCapital());
        }
}

Output:

[java][RemoteTestNG] detected TestNG version 6.14.2 Capital of India is New Delhi Capital of UK is London Capital of USA is New York PASSED: India(DataProvider.India@21213b92) PASSED: UK(DataProvider.UK@6f1fba17)

PASSED: USA(DataProvider.USA@67784306)

Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 1 – Introduction to REST Assured
Next Post: Are You Mixing Waits In Selenium WebDriver? – A Bad Practise

Related Posts

Frequently Asked Java Program 04: Java Program to Extract Numbers From String and Add Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
How To Use ThreadSafe Singleton Class To Manage Instance Variables In Automation Framework – Java Uncategorized
getAttribute() method in Selenium WebDriver – Why, What and How to use? Uncategorized
Accessibility Testing in Slides Uncategorized
Handling Non-Select or Any Types Of dropdown In Selenium Webdriver 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