DefaultResponseHandler implements ResponseHandler { public Object handle(Response response) throws Exception { if(response.getStatusCode() >= minErrorStatusCode) { throw new CRestException("Response Status Code:" + response.getStatusCode() + "\nResponse: " + response.to(String.class)); } return response.deserialize(); } DefaultResponseHandler(CRestConfig crestConfig); Object handle(Response response); static final String MIN_ERROR_STATUS_CODE_PROP; }
@Test public void shouldUseResponseDeserialize() throws Exception { Response response = mock(Response.class); when(response.deserialize()).thenReturn("hello"); Object actual = toTest.handle(response); assertEquals("hello", actual); }
@Test public void shouldThrowExceptionFor400PlusResponseStatus() throws Exception { Response response = mock(Response.class); when(response.getStatusCode()).thenReturn(123); when(response.to(String.class)).thenReturn("hello"); try { toTest.handle(response); fail(); } catch (CRestException e) { assertEquals("Response Status Code:123\nResponse: hello", e.getMessage()); } }