DataImporter { public void importTo(File folder, String fileName) { try { InputStream source = factory.openStream(); copyStreamToFileInFolder(folder, fileName, source); } catch (IOException e) { String message = MessageFormat.format("Could not import {0} into {1}.", fileName, folder.getName()); throw new UpdateFailedException(message, e); } } DataImporter(InputStreamFactory factory); void importTo(File folder, String fileName);  }
@Test public void closesStreamAfterOperation() throws Exception { ByteArrayInputStream stream = spy(new ByteArrayInputStream("Hallo".getBytes())); InputStreamFactory factory = mock(InputStreamFactory.class); when(factory.openStream()).thenReturn(stream); new DataImporter(factory).importTo(folder.getRoot(), "Test"); verify(stream).close(); }