WrappedResponseInputStream extends InputStream { @Override public void close() throws IOException { try { delegate.close(); } finally { ResponseHelper.closeResponseSilently(delegateResponse); } } WrappedResponseInputStream(
            @NonNull InputStream delegate, @NonNull Response delegateResponse); @Override int read(); @Override int read(final byte b[]); @Override int read(final byte b[], final int off, final int len); @Override long skip(final long n); @Override synchronized void mark(int readlimit); @Override synchronized void reset(); @Override int available(); @Override boolean markSupported(); @Override void close();  }
@Test public void closeInputStream_withNoException_delegateResponseIsClosed() throws Exception { final WrappedResponseInputStream streamUnderTest = new WrappedResponseInputStream(mockDelegateStream, mockRawResponseDelegate); streamUnderTest.close(); verify(mockRawResponseDelegate).close(); }
@Test public void closeInputStream_withException_delegateResponseIsClosed() throws Exception { final InputStream originalStream = mock(InputStream.class); final WrappedResponseInputStream streamUnderTest = new WrappedResponseInputStream(originalStream, mockRawResponseDelegate); doThrow(new ProcessingException("Exception")).when(originalStream).close(); try { streamUnderTest.close(); fail("ProcessingException should have been thrown"); } catch (Throwable t) { assertThat( "Exception thrown should be a ProcessingException", t, instanceOf(ProcessingException.class)); verify(mockRawResponseDelegate).close(); } }