HttpResponse { public String getContent() throws IOException { int contentLength = httpURLConnection.getContentLength(); if (contentLength == 0) { return ""; } if (Strings.isNullOrEmpty(content)) { content = CharStreams.toString(new InputStreamReader(getInputStream())); } return content; } HttpResponse(HttpURLConnection httpURLConnection); int getStatusCode(); InputStream getInputStream(); String getContent(); void disconnect();  }
@Test public void getContentReturnsErrorMessageWhenHttpURLConnectionReturnsErrorStream() throws Exception { String expected = "Hello, World! This is error message."; byte[] actual = expected.getBytes(); HttpURLConnection connection = mock(HttpURLConnection.class); when(connection.getContentLength()).thenReturn(actual.length); when(connection.getErrorStream()).thenReturn(new ByteArrayInputStream(actual)); HttpResponse httpResponse = new HttpResponse(connection); assertThat(httpResponse.getContent(), is(expected)); }