DefaultHttpResult implements HttpResult { public HttpResult body(String body) { try { response.getWriter().print(body); } catch (IOException e) { throw new ResultException(COULD_NOT_WRITE_TO_RESPONSE_BODY, e); } return this; } DefaultHttpResult(HttpServletResponse response, Status status); HttpResult addDateHeader(String name, long date); HttpResult addHeader(String name, String value); HttpResult addIntHeader(String name, int value); void sendError(int statusCode); void sendError(int statusCode, String message); void setStatusCode(int statusCode); void movedPermanentlyTo(String uri); T movedPermanentlyTo(final Class<T> controller); HttpResult body(String body); HttpResult body(InputStream body); HttpResult body(Reader body);  }
@Test public void shouldWriteStringBody() throws Exception { PrintWriter writer = mock(PrintWriter.class); when(response.getWriter()).thenReturn(writer); httpResult.body("The text"); verify(writer).print(anyString()); }
@Test public void shouldThrowResultExceptionIfAnIOExceptionWhenWriteStringBody() throws Exception { doThrow(new IOException()).when(response).getWriter(); try { httpResult.body("The text"); fail("should throw ResultException"); } catch (ResultException e) { } }
@Test public void shouldThrowResultExceptionIfAnIOExceptionWhenWriteInputStreamBody() throws Exception { doThrow(new IOException()).when(response).getOutputStream(); InputStream in = new ByteArrayInputStream("the text".getBytes()); try { httpResult.body(in); fail("should throw ResultException"); } catch (ResultException e) { } }
@Test public void shouldThrowResultExceptionIfAnIOExceptionWhenWriteReaderBody() throws Exception { doThrow(new IOException()).when(response).getWriter(); Reader reader = new StringReader("the text"); try { httpResult.body(reader); fail("should throw ResultException"); } catch (ResultException e) { } }