Browse Source

test: deleteCourse method in Course class

remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
ee86cf4325
  1. 40
      src/test/java/org/example/CourseTest.java

40
src/test/java/org/example/CourseTest.java

@ -6,6 +6,7 @@ import java.io.PrintStream;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.*; import java.io.*;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/** /**
* The CourseTest class contains unit tests for the Course class. * The CourseTest class contains unit tests for the Course class.
@ -131,6 +132,45 @@ class CourseTest {
deleteTempFile(); 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<Course> 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. * Creates a temporary file with the provided data.

Loading…
Cancel
Save