I try to pass CSV data to my test method.
This is my code: CSV file has 3 columns:
UserData.java
public class UserData {
private String TestNumber;
private String email;
private String FirstName;
//Getters and Setters
}
Dataprovider class:
@DataProvider(name = "customer" )
public static Object[][] getFilterValues() throws InvalidFormatException, IOException, InterruptedException, CsvException {
CSVReader reader= new CSVReaderBuilder(new FileReader("/home/dell/scripts/TEST/Automation - CSV/src/TestData/User.csv")).
withSkipLines(1).
build();
List<UserData> csv_objectList=reader.readAll().stream().map(data-> {
UserData csvObject= new UserData();
csvObject.setTestNumber(data[0]);
csvObject.setEmail(data[1]);
csvObject.setFirstName(data[2]);
return csvObject;
}).collect(Collectors.toList());
UserData[] cellData = csv_objectList.toArray(new UserData[csv_objectList.size()]);
Object[][] data = new Object[cellData.length][3];
for(int i = 0; i <cellData.length; i++) {
data[i][0] = cellData[i].getTestNumber();
data[i][1] = cellData[i].getEmail();
data[i][2] = cellData[i].getFirstName();
}
return data;
}
Test method:
public class UserCreate extends Base {
@Test( dataProvider = "customer", dataProviderClass = GetUserData.class)
public void UserCreate (UserData data) throws FileNotFoundException {
System.out.println(data.getTestNumber());
}
}
I tried this way to pass data. But I get this error:
[public void api.wapp.registation.UserCreate.UserCreate(utilities.UserData) throws java.io.FileNotFoundException] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation).
Data provider mismatch
Method: UserCreate([Parameter{index=0, type=utilities.UserData, declaredAnnotations=[]}])
Arguments: [(utilities.UserData) utilities.UserData@301ec38b,null,null]
Your test method expects UserData
as argument. But you dataprovider is providing String, String, String
. In the data provider method, you are passing the values of the UserData
into the object array instead of passing the UserData
object itself.
In the data provider method, what you have to do is that, put each UserData
object in a single Object[]
, which would be combined to a Object[][]
.
return csv_objectList.stream()
.map(user -> new Object[]{ user }) // this array represents a single test case
.toArray(Object[][]::new);