diff --git a/project-keeper/src/test/java/com/exasol/projectkeeper/githuboutput/FileOutputPublisherTest.java b/project-keeper/src/test/java/com/exasol/projectkeeper/githuboutput/FileOutputPublisherTest.java index 1ed97970..74a29d54 100644 --- a/project-keeper/src/test/java/com/exasol/projectkeeper/githuboutput/FileOutputPublisherTest.java +++ b/project-keeper/src/test/java/com/exasol/projectkeeper/githuboutput/FileOutputPublisherTest.java @@ -3,16 +3,21 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doThrow; -import java.io.IOException; -import java.io.UncheckedIOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; // [utest->dsn~verify-modes.output-parameters~1] +@ExtendWith(MockitoExtension.class) class FileOutputPublisherTest { @TempDir @@ -75,6 +80,25 @@ void appendExistingFile() throws IOException { assertThat(Files.readString(file), equalTo("existing\ncontent\nkey=value\n")); } + @Test + @SuppressWarnings("resource") // AutoClosable not closed by intention + void writeFails(@Mock final Writer writerMock) throws IOException { + final FileOutputPublisher publisher = new FileOutputPublisher(writerMock, Path.of("file")); + doThrow(new IOException("expected")).when(writerMock).write(ArgumentMatchers.any(String.class)); + final UncheckedIOException exception = assertThrows(UncheckedIOException.class, + () -> publisher.publish("key", "value")); + assertThat(exception.getMessage(), + equalTo("E-PK-CORE-189: Failed to write content 'key=value\n' to file 'file': 'expected'")); + } + + @Test + void closeFails(@Mock final Writer writerMock) throws IOException { + final FileOutputPublisher publisher = new FileOutputPublisher(writerMock, Path.of("file")); + doThrow(new IOException("expected")).when(writerMock).close(); + final UncheckedIOException exception = assertThrows(UncheckedIOException.class, publisher::close); + assertThat(exception.getMessage(), equalTo("E-PK-CORE-187: Failed to close 'file' after writing: 'expected'")); + } + OutputPublisher testee(final Path file) { return FileOutputPublisher.create(file); }