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