【发布时间】:2020-03-05 12:07:51
【问题描述】:
我有一个 java 方法检查文件是否存在..
public String checkFileExistance(String arg) throws IOException {
String FolderPath = SomePath
File file = new File(FolderPath);
if (file.exists() && file.isDirectory()) {
//Do something here
}
}
我想模拟 file.exist() 和 file.isDirectory() 让它返回 true 总是
我尝试了以下方法:
public void test_checkFileExistance1() throws IOException {
/**Mock*/
File mockedFile = Mockito.mock(File.class);
Mockito.when(mockedFile.exists()).thenReturn(true);
Mockito.when(mockedFile.isDirectory()).thenReturn(true);
/**Actual Call*/
ProcessClass.checkFileExistance("arg");
}
但它总是返回 false
【问题讨论】:
-
您没有在模拟对象上调用
exists()和isDirectory()。相反,您创建了一个new File(..)对象。 -
我不清楚你能详细说明吗?