diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index ec0f7c9..704edaa 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -15,6 +15,9 @@ import static org.junit.jupiter.api.Assertions.*; */ class CourseTest { private static final String TEMP_FILE = "test_course_data.txt"; + private static final Course sampleCourse = new Course("Math", "M101", 3); + private static final Course sampleCourse2 = new Course("Science", "S101", 4); + private static final Course sampleCourse3 = new Course("Java", "J101", 3); /** * Tests the printCourseInfo method of the Course class. @@ -22,12 +25,9 @@ class CourseTest { */ @Test void printCourseInfo() { - // Create a new Course object - Course course = new Course("Introduction to Programming", "CS101", 3); - // Set up the expected output - String expectedCourseName = "Course Name: Introduction to Programming"; - String expectedCourseCode = "Course Code: CS101"; + String expectedCourseName = "Course Name: Math"; + String expectedCourseCode = "Course Code: M101"; String expectedCourseCredits = "Course Credits: 3"; // Redirect System.out to capture printed output @@ -36,7 +36,7 @@ class CourseTest { System.setOut(new PrintStream(outputStream)); // Call the method to be tested - course.printCourseInfo(); + sampleCourse.printCourseInfo(); // Reset System.out System.setOut(originalOut); @@ -60,7 +60,7 @@ class CourseTest { @Test void writeToFile() { // Create a new course - Course course = new Course("Math", "C101", 3); + Course course = sampleCourse; // Write course data to the temporary file course.writeToFile(course.getCourseName(), course.getCourseCode(), course.getCourseCredit(), TEMP_FILE); @@ -72,7 +72,7 @@ class CourseTest { assertEquals(1, courses.size()); Course readCourse = courses.get(0); assertEquals("Math", readCourse.getCourseName()); - assertEquals("C101", readCourse.getCourseCode()); + assertEquals("M101", readCourse.getCourseCode()); assertEquals(3, readCourse.getCourseCredit()); // Delete the temporary file @@ -108,16 +108,12 @@ class CourseTest { deleteTempFile(); } - /** - * Tests the addCourse method of the Course class. - * Verifies that a new course can be added to the file. - */ @Test void addCourse() { // Create a new course - String courseName = "Math"; - String courseCode = "C101"; - int courseCredit = 3; + String courseName = sampleCourse.getCourseName(); + String courseCode = sampleCourse.getCourseCode(); + int courseCredit = sampleCourse.getCourseCredit(); // Write course data to the temporary file Course.addCourse(courseName, courseCode, courseCredit, TEMP_FILE); @@ -136,28 +132,19 @@ class CourseTest { deleteTempFile(); } - /** - * Tests the deleteCourse method of the Course class. - * Verifies that a course can be deleted from the file. - */ @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.println(sampleCourse.getCourseName()); + writer.println(sampleCourse.getCourseCode()); + writer.println(sampleCourse.getCourseCredit()); + writer.println(sampleCourse2.getCourseName()); + writer.println(sampleCourse2.getCourseCode()); + writer.println(sampleCourse2.getCourseCredit()); + writer.println(sampleCourse3.getCourseName()); + writer.println(sampleCourse3.getCourseCode()); + writer.println(sampleCourse3.getCourseCredit()); writer.close(); // Simulate user input for course deletion @@ -174,7 +161,7 @@ class CourseTest { assertEquals(2, coursesAfterDeletion.size()); // Check if the correct course was deleted - assertFalse(coursesAfterDeletion.contains(course1)); + assertFalse(coursesAfterDeletion.contains(sampleCourse)); deleteTempFile(); }