首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Getmethod Mokito java模拟

Getmethod Mokito java模拟
EN

Stack Overflow用户
提问于 2015-07-24 11:29:09
回答 1查看 1.1K关注 0票数 0

因此,我试图使用mockito来模拟getMethod请求/响应。但是我遇到了一些问题

代码语言:javascript
复制
@Mock
private HttpClient client;
private GetMethod method;

@InjectMocks
private WebserviceInterface webserviceInterface;

@Before
public void setUp() throws Exception {
    initMocks(this);
    setting up the a valid customer happens here

}


@Test
public void shouldReturnValidCustomerWithValidBarcode() throws Exception {
    // TODO: Mock out the ParcelService so that we can specify what JSON data is returned.
    // TODO: Create the Customer object that we expect
    // TODO: Call the method of module under test
    // TODO: assertThat(expected, is(theActualObject)

    when(client.executeMethod(any(HttpMethod.class))).thenReturn(200);

    String aValidCustomerJson = "JsonGoes Here";

    when(method.getResponseBodyAsString()).thenReturn(aValidCustomerJson);
   assertThat(webserviceInterface.parcelSearch("aValidBarcode"), is(aValidCustomer));


}

但是im得到一个空指针异常,我不知道为什么:

代码语言:javascript
复制
java.lang.NullPointerException at com.springapp.mvc.WebserviceInterfaceTest.shouldReturnValidCustomerWithValidBarcode(WebserviceInterfaceTest.java:137)

如果有任何帮助的话,谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-26 13:49:12

一般来说,不建议模拟外部库,因为您的测试代码将依赖于它们。最好创建一个删减层并对其进行模拟。在这种情况下,您可以将HttpClient包装在类中,这样就可以轻松地对其方法进行存根处理。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31609412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档