Browse Source

test: writeToFile method in Course class

remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
9a9e35153b
  1. 31
      src/test/java/org/example/CourseTest.java

31
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.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.*;
import java.util.List;
class CourseTest { class CourseTest {
@ -41,4 +44,32 @@ class CourseTest {
assertEquals(expectedCourseCode, outputLines[1].trim()); assertEquals(expectedCourseCode, outputLines[1].trim());
assertEquals(expectedCourseCredits, outputLines[2].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<Course> 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();
}
}
} }
Loading…
Cancel
Save