|
28 | 28 | import static org.junit.jupiter.api.Assertions.assertThrows; |
29 | 29 | import static org.junit.jupiter.api.Assertions.assertTrue; |
30 | 30 |
|
31 | | -import java.io.File; |
32 | 31 | import java.io.IOException; |
33 | 32 | import java.nio.file.Files; |
| 33 | +import java.nio.file.Path; |
34 | 34 | import lombok.SneakyThrows; |
35 | | -import org.junit.Rule; |
36 | 35 | import org.junit.jupiter.api.Assertions; |
37 | 36 | import org.junit.jupiter.api.Test; |
38 | | -import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport; |
39 | | -import org.junit.rules.TemporaryFolder; |
| 37 | +import org.junit.jupiter.api.io.TempDir; |
40 | 38 |
|
41 | 39 | /** SimpleFileWriterTest */ |
42 | | -@EnableRuleMigrationSupport |
43 | 40 | class SimpleFileWriterTest { |
44 | 41 |
|
45 | | - @Rule public final TemporaryFolder testFolder = new TemporaryFolder(); |
| 42 | + @TempDir private Path testFolder; |
46 | 43 |
|
47 | 44 | @Test |
48 | 45 | void testWriterNotNull() throws Exception { |
49 | | - final var temporaryFile = this.testFolder.newFile(); |
50 | | - new SimpleFileWriter(temporaryFile.getPath(), Assertions::assertNotNull); |
| 46 | + final var temporaryFilePath = Files.createFile(testFolder.resolve("testfile.txt")); |
| 47 | + new SimpleFileWriter(temporaryFilePath.toString(), Assertions::assertNotNull); |
51 | 48 | } |
52 | 49 |
|
53 | 50 | @Test |
54 | 51 | void testCreatesNonExistentFile() throws Exception { |
55 | | - final var nonExistingFile = new File(this.testFolder.getRoot(), "non-existing-file"); |
56 | | - assertFalse(nonExistingFile.exists()); |
| 52 | + final var nonExistingFilePath = testFolder.resolve("non-existing-file.txt"); |
| 53 | + assertFalse(nonExistingFilePath.toFile().exists()); |
57 | 54 |
|
58 | | - new SimpleFileWriter(nonExistingFile.getPath(), Assertions::assertNotNull); |
59 | | - assertTrue(nonExistingFile.exists()); |
| 55 | + new SimpleFileWriter(nonExistingFilePath.toString(), Assertions::assertNotNull); |
| 56 | + assertTrue(nonExistingFilePath.toFile().exists()); |
60 | 57 | } |
61 | 58 |
|
62 | 59 | @Test |
63 | 60 | void testContentsAreWrittenToFile() throws Exception { |
64 | 61 | final var testMessage = "Test message"; |
65 | 62 |
|
66 | | - final var temporaryFile = this.testFolder.newFile(); |
67 | | - assertTrue(temporaryFile.exists()); |
| 63 | + final var temporaryFilePath = Files.createFile(testFolder.resolve("testfile.txt")); |
| 64 | + assertTrue(temporaryFilePath.toFile().exists()); |
68 | 65 |
|
69 | | - new SimpleFileWriter(temporaryFile.getPath(), writer -> writer.write(testMessage)); |
70 | | - assertTrue(Files.lines(temporaryFile.toPath()).allMatch(testMessage::equals)); |
| 66 | + new SimpleFileWriter(temporaryFilePath.toFile().getPath(), writer -> writer.write(testMessage)); |
| 67 | + assertTrue(Files.lines(temporaryFilePath.toFile().toPath()).allMatch(testMessage::equals)); |
71 | 68 | } |
72 | 69 |
|
73 | 70 | @Test |
74 | 71 | @SneakyThrows |
75 | | - void testRipplesIoExceptionOccurredWhileWriting() { |
| 72 | + void testRipplesIoExceptionOccurredWhileWriting() throws Exception { |
76 | 73 | var message = "Some error"; |
77 | | - final var temporaryFile = this.testFolder.newFile(); |
| 74 | + final var temporaryFilePath = Files.createFile(testFolder.resolve("testfile.txt")); |
78 | 75 | assertThrows( |
79 | 76 | IOException.class, |
80 | 77 | () -> |
81 | 78 | new SimpleFileWriter( |
82 | | - temporaryFile.getPath(), |
| 79 | + temporaryFilePath.toString(), |
83 | 80 | writer -> { |
84 | 81 | throw new IOException("error"); |
85 | 82 | }), |
|
0 commit comments