### Question:
DisposableFileInputStream extends FileInputStream { @Override public void close() throws IOException { super.close(); if (!getFile().delete()) { throw new IOException("File " + file.getAbsolutePath() + " cannot be deleted."); } } DisposableFileInputStream(File file); @Override void close();  }### Answer:
@Test public void removeTempFileAfterClosing() throws IOException { DisposableFileInputStream input = new DisposableFileInputStream(tempFile); input.close(); Assert.assertFalse("TempFile must be removed", tempFile.exists()); }
@Test(expectedExceptions = IOException.class) public void exceptionWhenTempFileCannotBeDeleted() throws IOException { final File mockFile = Mockito.mock(File.class); Mockito.when(mockFile.delete()).thenReturn(false); DisposableFileInputStream input = new DisposableFileInputStream(tempFile) { @Override protected File getFile() { return mockFile; } }; input.close(); }