Getmethod Mokito java mock

Getmethod Mokito java mock,java,unit-testing,mocking,mockito,Java,Unit Testing,Mocking,Mockito,所以我尝试使用mockito来模拟getMethod请求/响应。但是我有一些问题 @Mock private HttpClient client; private GetMethod method; @InjectMocks private WebserviceInterface webserviceInterface; @Before public void setUp() throws Exception { initMocks(this); setting up the

所以我尝试使用mockito来模拟getMethod请求/响应。但是我有一些问题

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


}
但我收到一个空指针异常,我不确定原因:

java.lang.NullPointerException at com.springapp.mvc.WebserviceInterfaceTest.shouldReturnValidCustomerWithValidBarcode(WebserviceInterfaceTest.java:137)

如果有任何帮助,谢谢,一般来说,不建议模拟外部库,因为您的测试代码将依赖于它们。 最好创建一个抽象层并模拟它。
在您的情况下,您可以将HttpClient封装在一个类中,这样您就可以轻松地存根它的方法。

您的
方法
字段不也应该被注释
@Mock
吗?另外,(如果可能的话),您可能需要删除initMocks()赞成还是反对MockitoJUnitRunner:你能在抛出NPE的行中添加注释吗?过去让我感到困惑的是,如果我模拟的方法重载,那么它不会返回模拟的方法响应,它总是返回null。这绝对不是问题吗?