I'm dusting off my knowledge in Java, after a long time without taking the language, and I'm using OpenCSV for Java. I wrote a method that reads a CSV file with the following structure:
id,first_name,last_name,email,gender,ip_address,date
1,Bear,Picker,bpicker0@wordpress.com,Bigender,33.63.86.239,20/06/2021
2,Emili,McEntee,emcentee1@sciencedirect.com,Bigender,163.127.107.66,03/08/2021
3,Bethena,Fortune,bfortune2@live.com,Genderqueer,52.250.33.22,22/04/2021
4,Mortie,Praten,mpraten3@google.nl,Genderfluid,181.3.231.237,09/07/2021
5,Nappy,Giabucci,ngiabucci4@unc.edu,Female,110.180.139.101,01/06/2021
This is my basic method that I wrote to read the file:
public static List<String> readDataLineByLine(String path, Class classModel) {
try {
Reader reader = Files.newBufferedReader(Paths.get(path));
CsvToBean csvToBean = new CsvToBeanBuilder(reader)
.withType(classModel)
.withSeparator(',')
.withSkipLines(1)
.withIgnoreLeadingWhiteSpace(true)
.build();
List<String> results = csvToBean.parse();
return results;
} catch(IOException ex) {
Log.error(ex.toString());
return null;
}
}
My model class:
public class MockarooFakeModel {
@CsvBindByName(column = "id")
private int ID;
@CsvBindByName(column = "first_name")
private String firstName;
@CsvBindByName(column = "last_name")
private String lastName;
@CsvBindByName(column = "email")
private String email;
@CsvBindByName(column = "gender")
private String gender;
@CsvBindByName(column = "ip_address")
private String IPAddress;
@CsvBindByName(column = "date")
private String date;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getIPAddress() {
return IPAddress;
}
public void setIPAddress(String IPAddress) {
this.IPAddress = IPAddress;
}
public Date getDate() {
try {
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(this.date);
return date;
} catch (ParseException ex) {
Log.error(ex.toString());
return null;
}
}
public void setDate(String date) {
this.date = date;
}
}
For some reason, when I debug the code, it returns 4 rows instead of 5 rows, and also it returns the rows with null
. I thought it was because I didn't map the model, but that didn't fix the issue. Also, I don't think that TestNG causes some conflict when I execute the test.
@Test(description = "Gets the data stored in a CSV file")
public void Test4(Method method) {
startTest(method.getName(),"Gets the data stored in a CSV file");
var records = Utils.readDataLineByLine("PathToTheFile.csv", MockarooFakeModel.class);
}
I'll appreciate any help to solve my issue. Thanks!
Update
I forgot to check the checkbox to answer my own question, but I left here the solution to my problem (thanks,
I didn't know that .withSkipLines()
skipped the line of data that I was sending (and the mapping I did do that automatically).
CodePudding user response:
You described the problem very well, thanks for that!
In my opinion, the only problem is the line
.withSkipLines(1)
because you skip the header line while you are mapping with the headers at the same time. So the library is not able to match any value.
Just remove this from the lambda and you will get 5 lines of correctly mapped values.