What's the best approach to writing a unit test for a method that reads a file.
Should I be mocking the file like this?
File dumpFile = Mockito.mock(File.class);
Mockito.when(getDumpAsFile()).thenReturn(dumpFile);
Method Under Test
public List<String> getDumpAsList() {
CSVReader reader = null;
List<String> errors = new ArrayList<>();
try {
File f = getDumpAsFile();
reader = new CSVReader(f, "UTF-8");
reader.setLinesToSkip(0);
reader.setFieldSeparator(new char[] {','});
reader.setTextSeparator('"');
while(reader.readNextLine()) {
String line = reader.getSourceLine();
if (line != null && isErrorLine(line)) {
errors.add(line);
}
}
} catch (FileNotFoundException e) {
} catch (Exception e) {
throw new RuntimeException("Cannot extract dumped items", e);
} finally {
if (reader != null) {
reader.closeQuietly();
}
}
return errors;
}