FailureHandlingHttpClient extends AbstractHttpClientDecorator { public HttpResponse execute(HttpHost host, HttpRequest req, HttpContext ctx) throws IOException, ClientProtocolException { try { return backend.execute(host, req, ctx); } catch (UnsuccessfulResponseException ure) { return ure.getResponse(); } catch (CircuitBreakerException cbe) { throw new IOException(cbe); } } FailureHandlingHttpClient(HttpClient backend); HttpResponse execute(HttpHost host, HttpRequest req, HttpContext ctx);  }
@Test public void returnsBackendResponseOnSuccess() throws Exception { expect(mockBackend.execute(host, req, ctx)) .andReturn(resp); replay(mockBackend); HttpResponse result = impl.execute(host, req, ctx); verify(mockBackend); assertSame(resp, result); }
@Test public void returnsEnclosedResponseOnUnsuccessfulException() throws Exception { Exception e = new UnsuccessfulResponseException(resp); expect(mockBackend.execute(host, req, ctx)) .andThrow(e); replay(mockBackend); HttpResponse result = impl.execute(host, req, ctx); verify(mockBackend); assertSame(resp, result); }
@Test public void throwsIOExceptionForCircuitBreakerException() throws Exception { expect(mockBackend.execute(host, req, ctx)) .andThrow(new CircuitBreakerException()); replay(mockBackend); try { impl.execute(host, req, ctx); fail("should have thrown exception"); } catch (IOException expected) { } verify(mockBackend); }