Java 在 Mockito 中捕获参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3620796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capture an argument in Mockito
提问by Iker Jimenez
I'm testing a certain class. This class is internally instantiating a "GetMethod" object that gets passed to a "HttpClient" object that gets injected into the tested class.
我正在测试某个课程。这个类在内部实例化一个“GetMethod”对象,该对象被传递给一个“HttpClient”对象,该对象被注入到测试类中。
I'm mocking the "HttpClient" class, but I would need to modify the behaviour of one method of the "GetMethod" class too. I'm playing with ArgumentCaptor but I don't seem to be able to get a hold of the instantiated object in the "when" call.
我在嘲笑“HttpClient”类,但我也需要修改“GetMethod”类的一种方法的行为。我正在使用 ArgumentCaptor,但我似乎无法在“when”调用中获取实例化对象。
Example:
例子:
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));
Response:
回复:
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()
采纳答案by Iker Jimenez
Ok, this is how I've solved it. A little bit convoluted but couldn't find any other way.
好的,我就是这样解决的。有点复杂,但找不到任何其他方法。
In the test class:
在测试类中:
private GetMethod getMethod;
public void testMethod() {
when(mockHttpClient.executeMethod(any(GetMethod.class))).thenAnswer(new ExecuteMethodAnswer());
//Execute your tested method here.
//Acces the getMethod here, assert stuff against it.
}
private void setResponseStream(HttpMethodBase httpMethod, InputStream inputStream) throws NoSuchFieldException, IllegalAccessException {
Field privateResponseStream = HttpMethodBase.class.getDeclaredField("responseStream");
privateResponseStream.setAccessible(true);
privateResponseStream.set(httpMethod, inputStream);
}
private class ExecuteMethodAnswer implements Answer {
public Object answer(InvocationOnMock invocation) throws FileNotFoundException,
NoSuchFieldException, IllegalAccessException {
getMethod = (GetMethod) invocation.getArguments()[0];
setResponseStream(getMethod, new FileInputStream(source));
return HttpStatus.SC_OK;
}
}
回答by amorfis
You cant use when
on getMethod, because getMethod is not a mock. It is still real object created by your class.
您不能when
在 getMethod 上使用,因为 getMethod 不是模拟。它仍然是您的类创建的真实对象。
ArgumentCaptor has quite different purpose. Check section 15 here.
ArgumentCaptor 有完全不同的目的。在此处查看第 15 节。
You could make your code more testable. Generally, classes that are creating new instances of other classes are difficult to test. Put some factory to this class for creating get/post methods, then in test mock this factory, and make it mock get/post methods.
您可以使您的代码更具可测试性。通常,创建其他类的新实例的类很难测试。将一些工厂放入此类以创建 get/post 方法,然后在测试中模拟该工厂,并使其模拟 get/post 方法。
public class YourClass {
MethodFactory mf;
public YourClass(MethodFactory mf) {
this.mf = mf;
}
public void handleHttpClient(HttpClient httpClient) {
httpClient.executeMethod(mf.createMethod());
//your code here
}
}
Then in test you can do:
然后在测试中你可以这样做:
HttpClient mockHttpClient = mock(HttpClient.class);
when(mockHttpClient.executeMethod(any(GetMethod.class)).thenReturn(HttpStatus.SC_OK);
MethodFactory factory = mock(MethodFactory.class);
GetMethod get = mock(GetMethod.class);
when(factory.createMethod()).thenReturn(get);
when(get.getResponseBodyAsStream()).thenReturn(new FileInputStream(source));
UPDATE
更新
You can also try some nasty hack, and Answer
and accessing GetMethod's private parts ;) by reflection. (This is really nasty hack)
您还可以尝试一些讨厌的 hack,并Answer
通过反射访问 GetMethod 的私有部分 ;)。(这真的是令人讨厌的黑客)
when(mockHttpClient.executeMethod(any(GetMethod.class))).thenAnswer(new Answer() {
Object answer(InvocationOnMock invocation) {
GetMethod getMethod = (GetMethod) invocation.getArguments()[0];
Field respStream = HttpMethodBase.class.getDeclaredField("responseStream");
respStream.setAccessible(true);
respStream.set(getMethod, new FileInputStream(source));
return HttpStatus.SC_OK;
}
});