This class reads a file CSV.
public class ReadCSVFile {
private static final String SEMICOLON_DELIMITER = ";";
public Map<Integer,Company> listFromFile(String csvFile) throws IOException {
BufferedReader br = null;
br = new BufferedReader(new
InputStreamReader(ReadCSVFile.class.getResourceAsStream(csvFile)));
Map<Integer,Company> companyHashMap = new HashMap();
String line;
br.readLine();
while ((line = br.readLine()) != null) {
int pos = line.indexOf(SEMICOLON_DELIMITER);
String companyCode = line.substring(0,pos);
String companyName = line.substring(pos +1, line.length());
companyHashMap.put(Integer.parseInt(companyCode), new Company(Integer.parseInt(companyCode), companyName));
}
return companyHashMap;
}
}
This is the test for the class ReadCSVFile:
public class ReadCSVFileTest {
private ReadCSVFile readCSVFile;
@Before
public void before(){
readCSVFile = new ReadCSVFile();
}
@Test
public void shouldExtractCompanyFromCSV() throws IOException {
Map<Integer, Company> result = readCSVFile.listFromFile("test_company_list.csv");
Assert.assertEquals(2,result.size());
Assert.assertEquals("Goldman Sachs Group Inc",result.get(65).getCompanyName());
Assert.assertEquals("Repsol YPF SA (Please refer to Repsol SA and YPF SA)",result.get(66).getCompanyName());
}
at the end this is the file to read test_company_list.csv
and that I used to compare the result of the test:
RepRisk Company ID;Company Name
65;Goldman Sachs Group Inc
66;Repsol YPF SA (Please refer to Repsol SA and YPF SA)
The test fails, I have this message:
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at app.ReadCSVFile.listFromFile(ReadCSVFile.java:21)
at ReadCSVFileTest.shouldExtractCompanyFromCSV(ReadCSVFileTest.java:23)
What is wrong in my program? I think JUnit is correct as set up.
The line ReadCSVFile.java:21
is this one:
br = new BufferedReader(new InputStreamReader(ReadCSVFile.class.getResourceAsStream(csvFile)));
instead the line (ReadCSVFileTest.java:23)
:
Map<Integer, Company> result = readCSVFile.listFromFile("test_company_list.csv");
Aucun commentaire:
Enregistrer un commentaire