### Question:
AbstractHttpClientResponseHandler implements HttpClientResponseHandler<T> { @Override public T handleResponse(final ClassicHttpResponse response) throws IOException { final HttpEntity entity = response.getEntity(); if (response.getCode() >= HttpStatus.SC_REDIRECTION) { EntityUtils.consume(entity); throw new HttpResponseException(response.getCode(), response.getReasonPhrase()); } return entity == null ? null : handleEntity(entity); }  @Override T handleResponse(final ClassicHttpResponse response); abstract T handleEntity(HttpEntity entity);  }### Answer:
@Test public void testSuccessfulResponse() throws Exception { final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); final HttpEntity entity = new StringEntity("42"); Mockito.when(response.getCode()).thenReturn(200); Mockito.when(response.getEntity()).thenReturn(entity); final AbstractHttpClientResponseHandler<Integer> handler = new AbstractHttpClientResponseHandler<Integer>() { @Override public Integer handleEntity(final HttpEntity entity) throws IOException { return Integer.valueOf(new String(EntityUtils.toByteArray(entity))); } }; final Integer number = handler.handleResponse(response); Assert.assertEquals(42, number.intValue()); }
@SuppressWarnings("boxing") @Test public void testUnsuccessfulResponse() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final HttpEntity entity = Mockito.mock(HttpEntity.class); Mockito.when(entity.isStreaming()).thenReturn(true); Mockito.when(entity.getContent()).thenReturn(inStream); final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); Mockito.when(response.getCode()).thenReturn(404); Mockito.when(response.getReasonPhrase()).thenReturn("NOT FOUND"); Mockito.when(response.getEntity()).thenReturn(entity); final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); try { handler.handleResponse(response); Assert.fail("HttpResponseException expected"); } catch (final HttpResponseException ex) { Assert.assertEquals(404, ex.getStatusCode()); Assert.assertEquals("NOT FOUND", ex.getReasonPhrase()); Assert.assertEquals("status code: 404, reason phrase: NOT FOUND", ex.getMessage()); } Mockito.verify(entity).getContent(); Mockito.verify(inStream).close(); }
@SuppressWarnings("boxing") @Test public void testUnsuccessfulResponseEmptyReason() throws Exception { final InputStream inStream = Mockito.mock(InputStream.class); final HttpEntity entity = Mockito.mock(HttpEntity.class); Mockito.when(entity.isStreaming()).thenReturn(true); Mockito.when(entity.getContent()).thenReturn(inStream); final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); Mockito.when(response.getCode()).thenReturn(404); Mockito.when(response.getEntity()).thenReturn(entity); final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); try { handler.handleResponse(response); Assert.fail("HttpResponseException expected"); } catch (final HttpResponseException ex) { Assert.assertEquals(404, ex.getStatusCode()); Assert.assertNull(ex.getReasonPhrase()); Assert.assertEquals("status code: 404", ex.getMessage()); } Mockito.verify(entity).getContent(); Mockito.verify(inStream).close(); }