このクラスはファイルCSVを読み取ります。getResourceAsStreamメソッドはnullを返します。InputStream
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;
}
}
これはクラスReadCSVFileためのテストです:これはtest_company_list.csv
を読んで、私はテストの結果を比較するために使用されるファイルです最後に
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());
}
:
RepRisk Company ID;Company Name
65;Goldman Sachs Group Inc
66;Repsol YPF SA (Please refer to Repsol SA and YPF SA)
テストに失敗しました。このメッセージがあります:
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)
私のプログラムで何が間違っていますか?私はJUnitが正しいと思う。
ラインReadCSVFile.java:21
は、このいずれかになります。
br = new BufferedReader(new InputStreamReader(ReadCSVFile.class.getResourceAsStream(csvFile)));
代わりにライン(ReadCSVFileTest.java:23)
:
Map<Integer, Company> result = readCSVFile.listFromFile("test_company_list.csv");
。 2日前に正しく動作していたので、テストの奇妙な動作ですが、何も変更しませんでした。 – StoreCode