【问题标题】:Mocking HttpClient.execute issues: Mockito模拟 HttpClient.execute 问题:Mockito
【发布时间】:2013-10-28 03:07:23
【问题描述】:

我正在尝试测试这个方法。

@Override
public JSON connectResource() throws IOException {
    //get the location and credentials for the certificates
    System.setProperty("javax.net.ssl.trustStore", "C:/Program Files/Java/jdk1.7.0_40/jre/lib/security/cacerts");
    System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
    HttpRequest httpRequest = new HttpGet(url);
    System.out.println("hello");
    httpRequest.addHeader("Accept", "application/json");
    HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest);
    System.out.println("hello1");
    HttpEntity httpEntity = response.getEntity();
    String data = this.getData(httpEntity);
    return  JSONSerializer.toJSON(data.toString());
}

我的设置方法是:

@Before
public void setUp() throws Exception{
    mockHttpClient = mock(DefaultHttpClient.class);
    mockHttpRequest = mock(HttpUriRequest.class);
    mockHttpResponse = mock(BasicHttpResponse.class);
    mockHttpEntity = mock(HttpEntity.class);
    mockInputStream = mock(InputStream.class);
    mockInputStreamReader = mock(InputStreamReader.class);
    mockBufferedReader = mock(BufferedReader.class);
    mockHttpGet = mock(HttpGet.class);
    mockHttpRequestBase = mock(HttpRequestBase.class);
    //when(mockHttpClient.execute(Mockito.isA(HttpUriRequest.class))).thenReturn(mockHttpResponse);
    //when(mockHttpClient.execute(mockHttpRequest)).thenReturn(mockHttpResponse);
    //when(mockHttpClient.execute(mockHttpRequestBase)).thenReturn(mockHttpResponse);
    //when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);

    when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity);
    when(mockHttpEntity.getContent()).thenReturn(mockInputStream);
    PowerMockito.whenNew(InputStreamReader.class)
            .withArguments(mockInputStream).thenReturn(mockInputStreamReader);
    PowerMockito.whenNew(BufferedReader.class)
            .withArguments(mockInputStreamReader).thenReturn(mockBufferedReader);
    PowerMockito.when(mockBufferedReader.readLine())
            .thenReturn(JSON_STRING)
            .thenReturn(null);
    PowerMockito.whenNew(HttpGet.class).withArguments(VALID_URL)
            .thenReturn(mockHttpGet);
}

我的测试用例是:

 @Test
    public void testConnectResource() throws IOException {
        when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);
        HttpConnectGithub connHandle = new HttpConnectGithub(VALID_URL);
        JSON jsonObject = connHandle.connectResource();
        System.out.println(jsonObject);
        //assertThat(jsonObject, instanceOf(JSON.class));
    }

但是,执行在

处停止
HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest);

您可以在我的设置方法的 cmets 中看到我尝试的所有内容。 有没有人发现任何问题?我通过我的测试用例进行了调试,并且所有模拟对象都已正确初始化。 我曾尝试交换 HttpUriRequest 和 HttpRequest、HttpResponse 和 BasicHttpResponse 等,但运气不佳。 请指导如何解决此问题。

【问题讨论】:

    标签: http httpclient mockito apache-httpclient-4.x powermock


    【解决方案1】:

    您遇到的部分问题是参数匹配:

    @Test
    public void testConnectResource() throws IOException {
        when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);
        HttpConnectGithub connHandle = new HttpConnectGithub(VALID_URL);
        JSON jsonObject = connHandle.connectResource();
        System.out.println(jsonObject);
        //assertThat(jsonObject, instanceOf(JSON.class));
    }
    

    使用您在上面指定的行

    when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse);
    

    只有当你定义的 mockHttpGet 实例通过时才会触发。

    另一方面,您正在测试的方法是创建一个新的 HttpGet 实例,该实例与 mockHttpGet 实例不同。您将需要更改“when”语句,以便拥有类似

    的内容
    when(mockHttpClient.execute(Matchers.any(HttpGet.class))).thenReturn(mockHttpResponse);
    

    我完全凭记忆这样做,因此 Matchers.any() 可能不正确,但根据我上面给你的内容,你应该能够取得进展。

    【讨论】:

    • 匹配器是这里的关键,人们很容易忽略并想知道为什么模拟不起作用
    【解决方案2】:

    问题出在 mockHttpClient 上。由于某种原因,它无法自动模拟它。解决方法是通过某种方法(在我的情况下为构造函数)将 httpclient 作为参数传递

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 2014-06-16
      相关资源
      最近更新 更多