### Question:
FileConsumer implements OutputConsumer { @Override public void consumeOutput(List<Output> outputs) { try { Files.createDirectories(directory); for (Output output : outputs) { File outfile = Paths.get(directory.toString(), output.getName()).toFile(); FileOutputStream outStream = fileOutputStreamProvider.createStream(outfile); OutputStreamWriter writer = new OutputStreamWriter(outStream, StandardCharsets.UTF_8); writer.write(output.getData()); writer.close(); } } catch (IOException e) { throw new RuntimeException(e); } } FileConsumer(Path directory);  FileConsumer(@NonNull Path directory, FileOutputStreamProvider fileOutputStreamProvider); @Override void consumeOutput(List<Output> outputs);  }### Answer:
@Test void ioExceptionTest() throws FileNotFoundException { final IOException ioException = new IOException(); FileConsumer.FileOutputStreamProvider fileOutputStreamProvider = Mockito.mock(FileConsumer.FileOutputStreamProvider.class); Mockito.when(fileOutputStreamProvider.createStream(Mockito.any())).then(invocation -> { throw ioException; }); Output output = new Output("exception test", ""); FileConsumer fileConsumer = new FileConsumer(tempDir, fileOutputStreamProvider); assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> { fileConsumer.consumeOutput(Collections.singletonList(output)); }).withCause(ioException); }
@Test void simpleCreateTest() throws IOException { Output output = new Output("simple", "This is a simple test."); FileConsumer fileConsumer = new FileConsumer(tempDir); fileConsumer.consumeOutput(Collections.singletonList(output)); Path expectedFilePath = Paths.get(tempDir.toString(), output.getName()); assertThat(expectedFilePath.toFile().exists()).isTrue(); assertThat(Files.readAllLines(expectedFilePath)) .isEqualTo(Collections.singletonList(output.getData())); }
@Test void multipleTest() throws IOException { Output output1 = new Output("multiple1.md", "# First multiple test\n\nIf you can read this, the test was probably successful.\n"); Output output2 = new Output("multiple2", "You may delete these files (but not before the test finishes ;) )"); FileConsumer fileConsumer = new FileConsumer(tempDir); fileConsumer.consumeOutput(Arrays.asList(output1, output2)); Path expectedFilePath1 = Paths.get(tempDir.toString(), output1.getName()); assertThat(expectedFilePath1.toFile().exists()).isTrue(); String file1Content = new String(Files.readAllBytes(expectedFilePath1), StandardCharsets.UTF_8); assertThat(file1Content).isEqualTo(output1.getData()); Path expectedFilePath2 = Paths.get(tempDir.toString(), output2.getName()); assertThat(expectedFilePath2.toFile().exists()).isTrue(); String file2Content = new String(Files.readAllBytes(expectedFilePath2), StandardCharsets.UTF_8); assertThat(file2Content).isEqualTo(output2.getData()); }