From 9a9e35153ba435d8d72668333c62122046462aac Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:14:25 +0100 Subject: [PATCH] test: writeToFile method in Course class --- src/test/java/org/example/CourseTest.java | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 5445a3d..0a7d241 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -6,6 +6,9 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import static org.junit.Assert.assertEquals; +import java.io.*; +import java.util.List; + class CourseTest { @@ -41,4 +44,32 @@ class CourseTest { assertEquals(expectedCourseCode, outputLines[1].trim()); assertEquals(expectedCourseCredits, outputLines[2].trim()); } + + @Test + void writeToFile() { + // Create a temporary file for testing + String filename = "test_course_data.txt"; + + // Create a new course + Course course = new Course("Math", "C101", 3); + + // Write course data to the temporary file + course.writeToFile(filename); + + // Read course data from the temporary file + List courses = Course.readFromFile(filename); + + // Assert that the read data matches the original course + assertEquals(1, courses.size()); + Course readCourse = courses.get(0); + assertEquals("Math", readCourse.getCourseName()); + assertEquals(3, readCourse.getCourseCredit()); + + // Delete the temporary file + File file = new File(filename); + if (file.exists()) { + file.delete(); + } + + } } \ No newline at end of file