【问题标题】:Capture an argument in Mockito在 Mockito 中捕获参数
【发布时间】:2011-04-06 22:54:27
【问题描述】:

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

我在模拟“HttpClient”类,但我也需要修改“GetMethod”类的一种方法的行为。我正在使用 ArgumentCaptor,但我似乎无法在“何时”调用中获取实例化对象。

例子:

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

【问题讨论】:

    标签: java unit-testing testing mockito


    【解决方案1】:

    你不能在 getMethod 上使用when,因为 getMethod 不是一个 mock。它仍然是您的类创建的真实对象。

    ArgumentCaptor 有完全不同的目的。检查section 15 here

    您可以使您的代码更具可测试性。通常,创建其他类的新实例的类很难测试。为这个类添加一些工厂来创建 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
      }
    }
    

    那么在测试中你可以这样做:

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

    更新

    您还可以尝试一些讨厌的 hack,Answer 并通过反射访问 GetMethod 的私有部分;)。 (这真是令人讨厌的hack)

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

    【讨论】:

    • 我知道实例化使类难以测试,但在这种情况下,工厂将是矫枉过正,我不能随意更改实现太多。
    【解决方案2】:

    好的,我就是这样解决的。有点复杂,但找不到其他方法。

    在测试类中:

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

    【讨论】:

    • 您在我编辑答案时发布了它。好吧,我们都以同样的方式解决了它:)
    • 是的,我找不到使用可用工具的任何其他方法。讨厌的 hack :) 但它在工作时会摇摆不定!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多