我試圖嘲弄在單元測試情況如下功能:嘲諷Files.copy春季啓動單元測試
Files.copy(response.getBody(), pathObj, StandardCopyOption.REPLACE_EXISTING);
StandardCopyOption
是java.nio.file
包的枚舉:
的方式我想嘲笑是:
try {
Mockito.doNothing().when(Files.copy(Mockito.any(InputStream.class), Mockito.any(Path.class), Mockito.any(CopyOption.class)));
}catch (Exception e){
e.printStackTrace();
}
但我收到以下錯誤:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 3 recorded:
-> at scheduler.DownloadTest.downloadFile(DownloadTest.java:227)
-> at scheduler.DownloadTest.downloadFile(DownloadTest.java:227)
-> at scheduler.DownloadTest.downloadFile(DownloadTest.java:227)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
您可以使用內存中的文件系統嗎? https://github.com/google/jimfs –
mdewit完全正確;你不能使用Mockito來模擬靜態方法(也許你想花一些時間閱讀Mockito文檔;這樣的事情在那裏有明確的說明)。所以,唯一的兩個選擇是:讓你的不可測試的設計可測試;通過遵循這個答案;或轉向PowerMock(ito)。我強烈建議去第一個選項。 – GhostCat