加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在Mockito中捕获一个参数

发布时间:2020-12-14 23:55:09 所属栏目:Java 来源:网络整理
导读:我正在测试某个班级.该类在内部实例化一个“GetMethod”对象,该对象被传递给“HttpClient”对象,该对象被注入到测试类中. 我正在嘲笑“HttpClient”类,但我还需要修改“GetMethod”类的一个方法的行为.我正在玩ArgumentCaptor,但我似乎无法在“when”调用中
我正在测试某个班级.该类在内部实例化一个“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()

解决方法

你不能在getMethod上使用,因为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));

UPDATE

您还可以尝试一些讨厌的黑客,并通过反射回答和访问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;
  }
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读