TestNG — Passing ArrayList and Map in @Test method via @Dataprovider

Abhishek Dhoundiyal
2 min readFeb 26, 2023

--

Today, I came to a problem where I need to use theArrayList and Map data in the Test method.

Note: Although, we are passing Object[][] but later on the @Test method argument, we are using ArrayList and Map.

Example: The main use case is passing multiple ArrayList and Map based Object.

List<String> data = new ArrayList<>();
data.add("Abhishek");
data.add("Dh");
List<String> data2 = new ArrayList<>();
data.add("John");
data.add("Does");

Object[][] objData = new Object[1][2];
objData[0][0]= data;
objData[0][1]= data2;

Let’s understand, how we can do or implement this:

Using ArrayList:

Here, you can see I have created a 2 Dimension Object in

@Dataprovider and returned it. Object[][] objData = new Object[1][1]

And we have used the (List<String> data)in the test method argument.

@DataProvider(name = "List Data Test")
public Object[][] readListDetails() throws Exception {
List<String> data = new ArrayList<>();
data.add("Abhishek");
data.add("1234");
Object[][] objData = new Object[1][1];
objData[0][0]= data;
return objData;
}
@Test(dataProvider = "List Data Test")
public void dtListTestser(List<String> data){
System.out.println(data);
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i));
}
}

Output:

Using MAP:

Same goes for the below example.

@DataProvider(name = "Map Data Test")
public Object[][] readDetails() throws Exception {
HashMap<String,String> data = new HashMap<String,String>();
data.put("Name", "Abhishek");
data.put("ID", "1234");

Object[][] objData = new Object[1][1];
objData[0][0]= data;

return objData;
}
@Test(dataProvider = "Map Data Test")
public void dtTestser(HashMap<String,String> data){
System.out.println(data);
String API= data.get("Name");
System.out.println(data.get("ID"));
System.out.println(API);

}

Output:

--

--