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:

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

[/java]

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:

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

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}

		};

	}
}
Output:

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

[/java]

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 subclass objects by following Upcasting concept of Java.

Hope you are clear with above concept.

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

Leave a Reply

Your email address will not be published. Required fields are marked *