我正在测试某个类。这个类在内部实例化一个“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