From 1d31a533143a7ec90813978b35f7cadc8b08f4a7 Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Thu, 8 Feb 2024 15:29:08 +0100 Subject: [PATCH] refactoring: Added automatic creation/deletions of test.tx in StudentTest --- src/test/java/org/example/AdminTest.java | 5 ++-- src/test/java/org/example/StudentTest.java | 33 +++++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/example/AdminTest.java b/src/test/java/org/example/AdminTest.java index d9b7c80..40104f9 100644 --- a/src/test/java/org/example/AdminTest.java +++ b/src/test/java/org/example/AdminTest.java @@ -25,8 +25,7 @@ class AdminTest { public String fileName() { - String filename = "testAdminData.txt"; - return filename; + return "testAdminData.txt"; } //Tests printAdminInfo Method @@ -50,7 +49,7 @@ class AdminTest { @Test void writeToFile() throws IOException { Admin admin = new Admin("Simon", "A1001", "Admin") ; - + admin.writeToFile(admin, fileName()); Path path = Path.of(fileName()); diff --git a/src/test/java/org/example/StudentTest.java b/src/test/java/org/example/StudentTest.java index 2bac26a..1e5f76f 100644 --- a/src/test/java/org/example/StudentTest.java +++ b/src/test/java/org/example/StudentTest.java @@ -1,5 +1,7 @@ package org.example; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.*; @@ -10,17 +12,32 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; class StudentTest { + @BeforeEach + void setup() throws IOException { + // Ensure the file is clean before each test + new PrintWriter(fileName()).close(); + } + @AfterEach + void tearDown() { + // Clean up after tests + new File(fileName()).delete(); + } + + public String fileName() + { + return "testStudentData.txt"; + } private static final Student sampleStudent = new Student("Sebastian Reichard", "S1001", "Student"); @org.junit.jupiter.api.Test void writeToFile() throws IOException { Student student = sampleStudent; - String filename = "testStudentData.txt"; + // String filename = "testStudentData.txt"; - student.writeToFile(student, filename); + student.writeToFile(student, fileName()); - Path path = Path.of(filename); + Path path = Path.of(fileName()); String content = Files.readString(path); assertEquals("Sebastian Reichard\nS1001\nStudent", content); @@ -28,10 +45,6 @@ class StudentTest { } - - - - @Test void readFromFile() throws IOException { @@ -42,8 +55,8 @@ class StudentTest { } catch (IOException e) { e.printStackTrace(); } - String filename = "testStudentData.txt"; - List testStudents = Student.readFromFile(filename); + // String filename = "testStudentData.txt"; + List testStudents = Student.readFromFile(fileName()); assertEquals(1, testStudents.size()); Student admin = testStudents.get(0); @@ -52,7 +65,7 @@ class StudentTest { assertEquals("S1001", admin.getId()); assertEquals("Student", admin.getRole()); - Files.delete(Path.of(filename)); + // Files.delete(Path.of(filename)); }