diff --git a/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filewriter/FileWriterExampleUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filewriter/FileWriterExampleUnitTest.java index 5a8dfcc381..f94a73b023 100644 --- a/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filewriter/FileWriterExampleUnitTest.java +++ b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filewriter/FileWriterExampleUnitTest.java @@ -11,39 +11,37 @@ import org.junit.Test; public class FileWriterExampleUnitTest { - private static final String FILE_NAME = "src/test/resources/FileWriterTest.txt"; - @After public void tearDown() throws IOException { - Files.delete(Path.of(FILE_NAME)); + Files.delete(Path.of("src/test/resources/FileWriterTest.txt")); } @Test public void testWriteString() throws IOException { - try (FileWriter fileWriter = new FileWriter(FILE_NAME)) { + try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) { fileWriter.write("Hello Folks!"); } - Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of(FILE_NAME)))); + Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt")))); } @Test public void testAppendString() throws IOException { - try (FileWriter fileWriter = new FileWriter(FILE_NAME)) { + try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) { fileWriter.write("Hello Folks!"); } // using another try with resources to reopen the file in append mode - try (FileWriter fileWriter = new FileWriter(FILE_NAME, true)) { + try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", true)) { fileWriter.write("Hello Folks Again!"); } - Assert.assertEquals("Hello Folks!" + "Hello Folks Again!", new String(Files.readAllBytes(Path.of(FILE_NAME)))); + Assert.assertEquals("Hello Folks!" + "Hello Folks Again!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt")))); } @Test public void testWriteCharArray() throws IOException { - try (FileWriter fileWriter = new FileWriter(FILE_NAME)) { + try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) { fileWriter.write("Hello Folks!".toCharArray()); } - Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of(FILE_NAME)))); + Assert.assertEquals("Hello Folks!", new String(Files.readAllBytes(Path.of("src/test/resources/FileWriterTest.txt")))); } }