From ee86cf4325b42c0ed7c9b56d9f8e1dd253346b2d Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Wed, 7 Feb 2024 13:30:01 +0100 Subject: [PATCH] test: deleteCourse method in Course class --- src/test/java/org/example/CourseTest.java | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 5438509..fa987a0 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -6,6 +6,7 @@ import java.io.PrintStream; import static org.junit.Assert.assertEquals; import java.io.*; import java.util.List; +import static org.junit.jupiter.api.Assertions.*; /** * The CourseTest class contains unit tests for the Course class. @@ -131,6 +132,45 @@ class CourseTest { deleteTempFile(); } + @Test + public void testDeleteCourse() throws IOException { + // Prepare test data + Course course1 = new Course("Math", "M101", 3); + Course course2 = new Course("Science", "S101", 4); + Course course3 = new Course("Java", "J101", 4); + + // Write courses to the test file + PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE)); + writer.println(course1.getCourseName()); + writer.println(course1.getCourseCode()); + writer.println(course1.getCourseCredit()); + writer.println(course2.getCourseName()); + writer.println(course2.getCourseCode()); + writer.println(course2.getCourseCredit()); + writer.println(course3.getCourseName()); + writer.println(course3.getCourseCode()); + writer.println(course3.getCourseCredit()); + writer.close(); + + // Simulate user input for course deletion + ByteArrayInputStream inputStream = new ByteArrayInputStream("1\n0".getBytes()); + System.setIn(inputStream); + + // Delete a course + Course.deleteCourse(TEMP_FILE); + + // Read courses after deletion + List coursesAfterDeletion = Course.readFromFile(TEMP_FILE); + + // Ensure the correct number of courses remaining after deletion + assertEquals(2, coursesAfterDeletion.size()); + + // Check if the correct course was deleted + assertFalse(coursesAfterDeletion.contains(course1)); + + deleteTempFile(); + } + /** * Creates a temporary file with the provided data.