2013-10-28 158 views
-1

我想測試這種方法。嘲笑HttpClient.execute問題:Mockito

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

你可以看到所有我在設置方法的評論中嘗試過。 有沒有人發現任何問題?我通過測試用例進行了調試,所有模擬對象都已正確初始化。 我曾嘗試交換HttpUriRequest和HttpRequest,HttpResponse和BasicHttpResponse等,但沒有多少運氣。 請指導如何解決這個問題。

回答

1

問題在於mockHttpClient。由於某些原因,它無法自動嘲笑它。解決的辦法是通過一些方法作爲參數傳遞httpclient(在我的情況下是構造函數)

4

你正在運行到是匹配的參數問題的一部分:

@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(mockHttpClient.execute(Matchers.any(HttpGet.class))).thenReturn(mockHttpResponse); 

我這樣做是完全從內存中,因此Matchers.any()可能是不正確的改變「何時」的語句,但你應該能夠取得進展基於我上面給你的。