Passing multiple object in dataprovider in Testng

When we want to pass an object in TestNg data provider we use to follow the below steps.





package com.dokimi.tests.ui;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestClass {
private String dob = "xyz";
public String getDob() {
return dob;
}
@DataProvider
public Object[][] test() {
TestClass test = new TestClass();
return new Object[][]{
{"prem", "kumar", test}
};
}
@Test(dataProvider = "test")
public void testing(String firstname, String secondName, TestClass test) {
System.out.println("First Name::" + firstname);
System.out.println("Second Name::" + secondName);
System.out.println("Test Object::" + test.getDob());
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

But This can be rewritten in this way for a cleaner code 




package com.dokimi.tests.ui;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestClass {
private String dob = "xyz";
public String getDob() {
return dob;
}
@DataProvider
public Iterator<Object[]> test() {
List<Object[]> list = new ArrayList<>();
TestClass test = new TestClass();
for(int i=0;i<3;i++) {
list.add(new Object[]{"prem"+i,"kumar"+i,test});
}
return list.iterator();
}
@Test(dataProvider = "test")
public void testing(String firstname, String secondName, TestClass test) {
System.out.println("First Name::" + firstname);
System.out.println("Second Name::" + secondName);
System.out.println("Test Object::" + test.getDob());
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

Convert resultset object to list of map

Convert the resultset object to list of map In some cases, while we doing API automation, we need to fetch the value from the DB and hav...