在Mockito中捕捉一个论点

时间:2010-09-01 17:55:08

标签: java unit-testing testing mockito

我正在测试某个班级。这个类在内部实例化一个“GetMethod”对象,该对象被传递给一个“HttpClient”对象,该对象被注入到被测试的类中。

我正在嘲笑“HttpClient”类,但我还需要修改“GetMethod”类的一个方法的行为。我正在玩ArgumentCaptor,但我似乎无法在“when”调用中获取实例化对象。

示例:

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()

2 个答案:

答案 0 :(得分:12)

你不能在getMethod上使用when,因为getMethod不是mock。它仍然是你班级创造的真实物品。

ArgumentCaptor有着截然不同的目的。检查section 15 here

您可以使代码更易于测试。通常,创建其他类的新实例的类很难测试。把一些工厂放到这个类来创建get / post方法,然后在test mock中修改这个工厂,并使它成为模拟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
  }
}

然后在测试中你可以做到:

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));

更新

你也可以通过反思尝试一些讨厌的黑客和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;
  }
});

答案 1 :(得分:4)

好的,这就是我解决它的方式。有点复杂,但找不到任何其他方式。

在测试课程中:

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;
    }
}