Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 53: DataProvider in TestNG – Is It Mandatory To Have Return Type as Object in DataProvider Method

Posted on 03/21/2025 By admin

Hello Guys,

DataProvider method in TestNG is a way to provide test data to Test annotated methods in a TestNG class. A typical DataProvider method looks like as below:

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

        };

}

You can see that return type of above DataProvider method is an Object array. Is it mandatory to have return type as Object only? Answer is NO. But I see some people understands that it should be Object always which is not correct.

In fact you can have return type of a DataProvider method other than Object as well. If your all data is of type String, use String. See an example below:

package DataProvider;

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

public class DataProviderWithReturnType {

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

 }

 // A data provider method with return type as 2D array
 @DataProvider(name = "DataContainer")
 public static String[][] myDataProvider() {
  return new String[][] {
   {
    "Mukesh",
    "Otwani",
    "[email protected]"
   }, {
    "Amod",
    "Mahajan",
    "[email protected]"
   }, {
    "Animesh",
    "Prashant",
    "[email protected]"
   }, {
    "Ankur",
    "Singh",
    "[email protected]"
   }, {
    "Amritansh",
    "Kumar",
    "[email protected]"
   }

  };

 }
}

Output:

[java] [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]”)

Uncategorized

Post navigation

Previous Post: Does getWindowHandles() method return handles in the same order as windows launch?
Next Post: Selenium Framework 5: Understand Keyword Driven Framework in Selenium

Related Posts

image – Make Selenium Easy Uncategorized
REST Assured Tutorial 75 – What Is Serialization And Deserialization In Rest Assured? Uncategorized
Git Tutorial 11 – Git Pull – Download And Merge Changes From Remote Repository Uncategorized
Bedsheets – Make Selenium Easy Uncategorized
Make Selenium Easy – Page 2 of 57 – Uncategorized
December 22, 2018 – Make Selenium Easy 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