0
hw嘲笑以下特別是listfiles()
方法?嘲笑文件對象Mockito或Powermock?
File folder = new File(folderPath);
for (File fileEntry : folder.listFiles()) {
try {
if (StringUtils.isBlank(fileEntry.toString())) {
throw new ConfigurationException("Sfile must be set");
}
String json = resourceReader.readResource(fileEntry.toString());
try {
config.add(parser.parseJson(json));
} catch (Exception e) {
LOG.error("Error in parsing json");
}
} catch (Exception e) {
throw new ServiceException("Could not parse sfrom the file: " +
fileEntry.getName(), e);
}
}
剛回來一些'Iterable''有什麼問題?你遇到的確切問題是什麼? –
Turing85
我正在做這樣的事情。 @Mock File mockFile = Mockito.mock(File.class); File [] files = {admin,database}; Mockito.when(mockFile.listFiles())。然後返回(文件); – Yahiya
如果你正在使用Powermock,你可以在調用'new File(...)'時返回一個模擬'File'。或者你可以改變你的代碼來讓工廠返回你的文件(或者作爲參數傳遞它,然後你可以從你的單元測試中通過模擬並嘲笑'listFiles') – BretC