我遇到了一些奇怪的事情。我有一种方法可以逐行读取 CSV 文件。该方法采用文件路径,在我的 JUnit 测试中,我使用错误的文件路径测试此方法,期望得到 FileNotFoundException。问题是 JUnit5 不会抛出该异常,但在 eclipse 控制台中我可以看到 JVM 抛出该异常,所以我很难理解为什么
我已经设置了我的测试代码来抛出异常,但它没有被抛出。我试图捕捉异常但仍然没有快乐。
这是方法和测试方法
public void readData(String COMMA_DELIMITER, String READ_FILE_PATH) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(READ_FILE_PATH));
String line = "";
//Read to skip the header
br.readLine();
//Reading from the second line
while ((line = br.readLine()) != null)
{
String[] employeeDetails = line.split(COMMA_DELIMITER);
populateModel(employeeDetails);
}
//Lets print the Employee List
for(Employee e : empList)
{
System.out.println(e.getName() + "; " + e.getSurname() + "; " + e.getDateOfBirth() + "; " + e.getSex());
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
@Test
void testWrongFilePath() {
String READ_FILE_PATH_WRONG = System.getProperty("user.dir") + "/teest/XXXFile.csv";
System.out.println(READ_FILE_PATH_WRONG);
Assertions.assertThrows(FileNotFoundException.class, () -> {
readData.readData(COMMA_DELIMITER, READ_FILE_PATH_WRONG);
});
}
在控制台中,我得到了 FIleNotFoundException,但测试的输出表明
org.opentest4j.AssertionFailedError: Expected java.io.FileNotFoundException to be thrown, but nothing was thrown.
慕容森
慕田峪4524236
繁华开满天机
慕勒3428872
相关分类