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 | Make Selenium Easy

Posted on 12/17/2018 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:

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

Note: You can not have return type as primitive e.g. int etc. If you use it, you will get class cast exception. Refer 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(int n1, int n2, int n3) {
  System.out.println("n1 =" + n1 + " n2 =" + n2 + " n3 =" + n3);

 }

 // A data provider method with return type as 2D array
 @DataProvider(name = "DataContainer")
 public static int[][] myDataProvider() {
  return new int[][] {
   {
    1,
    2,
    3
   }, {
    3,
    4,
    5
   }

  };

 }
}

Output:

[RemoteTestNG] detected TestNG version 6.14.2
FAILED: studentRegistration
java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;

Solution is that you need to use wrapper class of primitive. E.g. Integer for int.

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(int n1, int n2, int n3) {
                System.out.println("n1 ="+n1 + " n2 ="+n2 +" n3 ="+n3);

        }

        // A data provider method with return type as 2D array
        @DataProvider(name = "DataContainer")
        public static Integer[][] myDataProvider() {
                return new Integer[][] { { 1,2,3 },
                                { 3,4,5}

                };

        }
}
[RemoteTestNG] detected TestNG version 6.14.2
n1 =1 n2 =2 n3 =3
n1 =3 n2 =4 n3 =5
PASSED: studentRegistration(1, 2, 3)
PASSED: studentRegistration(3, 4, 5)

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


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

So it’s clear now that a DataProvider method can return other types as well not only Object. Return type as Object is preferred when we need to return heterogenous data. E.g Integer, String etc all from one method. Since Object class is super class of all other classes in Java so Object class can provide reference to sub class objects by following Upcasting concept of Java.

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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 24: Annotations In TestNG – A Quick Guide | Make Selenium Easy
Next Post: Make Selenium Easy | Make Selenium Easy – Selenium Tutorials : End To End

Related Posts

Css – Make Selenium Easy Uncategorized
IMG_6689[1] – Make Selenium Easy Uncategorized
Handling JQuery Dialog Box In Selenium Webdriver Uncategorized
Advanced TestNG Tutorials 35: How To Pass Multiple Group Names to be Run at Runtime in TestNG XML Using Beanshell Uncategorized
XPath Method – string() – Usage in Locating Element- How it is Different From text() Uncategorized
Selenium Topics – Page 20 – 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