Categories: Selenium Topics

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

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", "Motwani@gmail.com" },
                { "Amod", "Mahajan", "Amahajan@hotmail.com" }, 
                { "Animesh", "Prashant", "aprashant@gmail.com" },
                { "Ankur", "Singh", "asingh@gmail.com" }, 
                { "Amritansh", "Kumar", "akumar@gmail.com" }

        };

}

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",
    "Motwani@gmail.com"
   }, {
    "Amod",
    "Mahajan",
    "Amahajan@hotmail.com"
   }, {
    "Animesh",
    "Prashant",
    "aprashant@gmail.com"
   }, {
    "Ankur",
    "Singh",
    "asingh@gmail.com"
   }, {
    "Amritansh",
    "Kumar",
    "akumar@gmail.com"
   }

  };

 }
}

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

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.


                

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

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

Recent Posts

How to Validate Field Color in Selenium WebDriver – Validation for Mandatory FIeld or Valid values

Hello Folks, Color code may represents an information about field. Consider below scenarios: All mandatory field of a web form…

4 days ago

Advanced Concept of Selenium – Design Patterns in Selenium WebDriver – There is Not Only Page Object Model Design Pattern

Hello Folks, When we talk about design pattern in Selenium WebDriver, we generally discuss about Page Object Model (POM) and…

4 days ago

TestNG Tutorials 64: Dependency in TestNG – ignoreMissingDependencies – Another Way of Achieving Soft Dependencies

Hello Folks, There are two types of dependencies in TestNG: 1. Hard Dependency : All the methods you depend on must have…

5 days ago

API Testing Tutorial Part 14 – Installation of Postman Tool

Hello Folks, As part of our API Testing series, we will see “Installation of Postman Tool” in this post. Postman tool was…

5 days ago

TestNG Tutorials 63: Dependency in TestNG – Usage of Regular Expressions with DependsOnGroup

Hello Folks, We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below:…

1 week ago

Frequently Asked Java Program 25: Java Program to Convert a String Sentence in Camel Case

"CamelCase" is a naming convention in which words are joined together without any whitespace in between and each word starts…

2 weeks ago