HttpResponse { public InputStream getInputStream() { return inputStream; } HttpResponse(HttpURLConnection httpURLConnection); int getStatusCode(); InputStream getInputStream(); String getContent(); void disconnect();  }
@Test public void getInputStreamReturnsErrorStreamWhenHttpURLConnectionReturnsErrorStream() throws Exception { InputStream expected = new ByteArrayInputStream(new byte[0]); HttpURLConnection connection = mock(HttpURLConnection.class); when(connection.getErrorStream()).thenReturn(expected); HttpResponse httpResponse = new HttpResponse(connection); InputStream actual = httpResponse.getInputStream(); assertThat(actual, is(not(nullValue()))); assertThat(actual, is(sameInstance(expected))); }
@Test public void getInputStreamReturnsInputStreamWhenHttpURLConnectionReturnsInputStream() throws Exception { InputStream expected = new ByteArrayInputStream(new byte[0]); HttpURLConnection connection = mock(HttpURLConnection.class); when(connection.getInputStream()).thenReturn(expected); HttpResponse httpResponse = new HttpResponse(connection); InputStream actual = httpResponse.getInputStream(); assertThat(actual, is(not(nullValue()))); assertThat(actual, is(sameInstance(expected))); }
@Test public void getInputStreamReturnsErrorStreamWhenHttpConnectionReturnsBothErrorStreamAndInputStream() throws Exception { InputStream inputStream = new ByteArrayInputStream(new byte[0]); InputStream errorStream = new ByteArrayInputStream(new byte[0]); HttpURLConnection connection = mock(HttpURLConnection.class); when(connection.getErrorStream()).thenReturn(errorStream); when(connection.getInputStream()).thenReturn(inputStream); HttpResponse httpResponse = new HttpResponse(connection); InputStream actual = httpResponse.getInputStream(); assertThat(actual, is(not(nullValue()))); assertThat(actual, is(sameInstance(errorStream))); }