我正在測試某個類。這個類在內部實例化一個「GetMethod」對象,該對象被傳遞給被注入到被測試類中的「HttpClient」對象。我正在嘲笑「HttpClient」類,但我需要修改「GetMethod」類的一個方法的行爲。我在玩ArgumentCaptor,但我似乎無法在「何時」調用中獲得實例化對象。在Mockito中捕獲一個參數
例子:
HttpClient mockHttpClient = mock(HttpClient.class);
ArgumentCaptor<GetMethod> getMethod = ArgumentCaptor.forClass(GetMethod.class);
when(mockHttpClient.executeMethod(getMethod.capture())).thenReturn(HttpStatus.SC_OK);
when(getMethod.getValue().getResponseBodyAsStream()).thenReturn(new FileInputStream(source));
響應:
org.mockito.exceptions.base.MockitoException:
No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()
您在編輯我的答案時發佈了它。那麼,我們都以同樣的方式解決它:) – amorfis 2010-09-02 15:59:01
是的,我找不到任何其他方式使用可用的工具。令人討厭的黑客:)但它的工作時,它岩石! – 2010-09-02 23:06:43