Browse Source

test: readFromFile method in Course class

remotes/origin/fdai7780
fdai7921 11 months ago
parent
commit
2a2caa891e
  1. 39
      src/test/java/org/example/CourseTest.java

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

@ -1,7 +1,5 @@
package org.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@ -70,6 +68,43 @@ class CourseTest {
if (file.exists()) {
file.delete();
}
}
@Test
void readFromFile() {
// Create a temporary file with sample course data
String filename = "test_course_data.txt";
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
writer.println("Math");
writer.println("C101");
writer.println(3);
writer.println("Science");
writer.println("C201");
writer.println(4);
} catch (IOException e) {
e.printStackTrace();
}
// Read course data from the temporary file
List<Course> courses = Course.readFromFile(filename);
// Assert that the read data matches the original course data
assertEquals(2, courses.size());
Course course1 = courses.get(0);
assertEquals("Math", course1.getCourseName());
assertEquals("C101", course1.getCourseCode());
assertEquals(3, course1.getCourseCredit());
Course course2 = courses.get(1);
assertEquals("Science", course2.getCourseName());
assertEquals("C201", course2.getCourseCode());
assertEquals(4, course2.getCourseCredit());
// Delete the temporary file
File file = new File(filename);
if (file.exists()) {
file.delete();
}
}
}
Loading…
Cancel
Save