HttpClient.execute: Mockito Problems

I am trying to test this method.

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

My setup method:

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

And my test case:

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

However, execution stops at

 HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest); 

You can see everything that I tried in the comments of my setup method. Can anyone find something in this question? I debugged my test case and all the false objects were correctly initialized. I tried to exchange HttpUriRequest and HttpRequest, HttpResponse and BasicHttpResponse etc., but without much luck. Read how to solve this problem.

-1
source share
2 answers

The problem is mockHttpClient. For some reason, he cannot mock him automatically. The fix is ​​to pass httpclient as a parameter through some method (constructor in my case)

+1
source

Part of the problem you're working with is consistent with the arguments:

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

With the line above

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

The mocking will only fire when the mockHttpGet instance that you defined is passed.

On the other hand, your test method creates a new HttpGet instance that will not be the same as the mockHttpGet instance. You will need to change the "when" instruction so that you have something like

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

I do this solely from memory, so Matchers.any () may be wrong, but you should be able to take a step forward based on what I gave you above.

+4
source

All Articles