### Question:
JenkinsClient { protected <T> T execute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor) throws JenkinsClientException { ClientHttpResponse response = null; try { ClientHttpRequest request = clientHttpRequestFactory.createRequest(url, method); if (requestCallback != null) { requestCallback.doWithRequest(request); } response = request.execute(); if (getResponseErrorHandler().hasError(response)) { getResponseErrorHandler().handleError(response); } if (responseExtractor != null) { return responseExtractor.extract(response); } else { return null; } } catch (IOException e) { throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + url + "\": " + e.getMessage(), e); } finally { if (response != null) { response.close(); } } } JenkinsClient();  JenkinsClient(String host, Integer port);  JenkinsClient(String scheme, String host, Integer port, String prefix); T getForObject(String uri, Class<T> responseType); URI postForLocation(String uri, Object request); T postForObject(String uri, Object request, Class<T> responseType); JenkinsClient setCredentials(String user, String password); JenkinsClient setUserAgent(String identifier); ClientHttpRequestFactory getClientHttpRequestFactory(); void setClientHttpRequestFactory(ClientHttpRequestFactory clientHttpRequestFactory); ResponseErrorHandler getResponseErrorHandler(); JenkinsClient setResponseErrorHandler(ResponseErrorHandler responseErrorHandler); List<HttpMessageConverter<?>> getMessageConverters(); void setMessageConverters(List<HttpMessageConverter<?>> messageConverters); static final String DEFAULT_URL_HOST; static final int DEFAULT_URL_PORT; static final String SEGMENT_JOB; static final String SEGMENT_CREATE_ITEM; static final String SEGMENT_DO_DELETE; static final String SEGMENT_API_XML; static final String SEGMENT_CONFIG_XML; static final String SEGMENT_SCRIPT_TEXT; static final String DEFAULT_USER_AGENT; }### Answer:
@Test public void execute_WhenErrorHandlerThrowsException_ThrowsExceptionAndClosesHttpResponse() throws Exception { given(httpRequestFactory.createRequest(new URI("http: given(errorHandler.hasError(httpResponse)).willReturn(true); willThrow(new HttpServerErrorException(INTERNAL_SERVER_ERROR)).given(errorHandler).handleError(httpResponse); given(httpRequest.execute()).willReturn(httpResponse); try { client.execute(new URI("http: fail(); } catch (HttpServerErrorException expected) { assertEquals(INTERNAL_SERVER_ERROR, expected.getStatusCode()); } verify(httpResponse).close(); }
@Test public void execute_WhenThrowsIOException_ThrowsException() throws Exception { given(httpRequestFactory.createRequest(new URI("http: willThrow(new IOException("Unplugged network cable")).given(httpRequest).execute(); try { client.execute(new URI("http: fail(); } catch (ResourceAccessException expected) { assertEquals("I/O error on GET request for \"http: expected.getMessage()); } }