Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Mar 8, 2024
1 parent 8e89865 commit 21b98d3
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 21b98d3

Please sign in to comment.