![](/img/trans.png)
[英]Mocking Local Scope Objects using Powermock and mockito within a static method
[英]Mocking file objects Mockito or Powermock?
為何要專門模擬以下listfiles()
方法?
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);
}
}
假設您在測試目錄中有一些文件,或者添加了一些.txt或其他文件。 如果您使用的是Mockito,請在測試類中編寫這些語句。 您需要添加import靜態org.mockito.Mockito.when; 在測試班
File f = new File("c:/test");
File[] files = f.listFiles();
when(folder.listFiles()).thenReturn(files);
這樣,您告訴測試方法進入實際類的for循環中,並且可能需要編寫更多一些when(thisHappens).thenReturn(givemethisresult);。 陳述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.