### Question:
TempFileContentKeeper implements ContentKeeper { @Override public InputStream getInputStream() throws FileDownloadException { try { File contentFile = fileWrapper().createTempFile("dbdump", contentProvider.getContentFileNameExt()); OutputStream output = newFileOutputStream(contentFile); contentProvider.writeContent(output); output.flush(); output.close(); return newFileInputStream(contentFile); } catch (IOException e) { throw new ContentPersistenceException(e); } } TempFileContentKeeper(ContentProvider contentProvider); @Override InputStream getInputStream(); @Override String getMimeContentType(); @Override String getContentFileNameExt();  }### Answer:
@Test public void writesContentToProvidedOutput() throws Exception { FileWrapper mockFileWrapper = mock(FileWrapper.class); OutputStream mockOutput = mock(OutputStream.class); InputStream mockInput = mock(InputStream.class); sut = getSpyForGetInputStreamMethod(mockFileWrapper, mockOutput, mockInput); sut.getInputStream(); verify(mockContentProvider).writeContent(mockOutput); }
@Test public void closesOutputAfterFinishWriting() throws Exception { FileWrapper mockFileWrapper = mock(FileWrapper.class); OutputStream mockOutput = mock(OutputStream.class); InputStream mockInput = mock(InputStream.class); sut = getSpyForGetInputStreamMethod(mockFileWrapper, mockOutput, mockInput); sut.getInputStream(); InOrder inOrder = inOrder(mockContentProvider, mockOutput); inOrder.verify(mockContentProvider).writeContent(mockOutput); inOrder.verify(mockOutput).flush(); inOrder.verify(mockOutput).close(); }
@Test public void returnsInputStreamForContent() throws Exception { FileWrapper mockFileWrapper = mock(FileWrapper.class); OutputStream mockOutput = mock(OutputStream.class); InputStream mockInput = mock(InputStream.class); sut = getSpyForGetInputStreamMethod(mockFileWrapper, mockOutput, mockInput); assertEquals(sut.getInputStream(), mockInput); }
@Test(expectedExceptions = ContentPersistenceException.class) public void throwsContentPersistenceExceptionIfIOError() throws Exception { FileWrapper mockFileWrapper = mock(FileWrapper.class); OutputStream mockOutput = mock(OutputStream.class); InputStream mockInput = mock(InputStream.class); doThrow(IOException.class).when(mockOutput).close(); sut = getSpyForGetInputStreamMethod(mockFileWrapper, mockOutput, mockInput); sut.getInputStream(); }