|
|
@ -1,5 +1,7 @@ |
|
|
|
package org.example; |
|
|
|
|
|
|
|
import org.junit.jupiter.api.AfterEach; |
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
@ -10,17 +12,32 @@ import java.util.List; |
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
class StudentTest { |
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
void setup() throws IOException { |
|
|
|
// Ensure the file is clean before each test |
|
|
|
new PrintWriter(fileName()).close(); |
|
|
|
} |
|
|
|
@AfterEach |
|
|
|
void tearDown() { |
|
|
|
// Clean up after tests |
|
|
|
new File(fileName()).delete(); |
|
|
|
} |
|
|
|
|
|
|
|
public String fileName() |
|
|
|
{ |
|
|
|
return "testStudentData.txt"; |
|
|
|
} |
|
|
|
private static final Student sampleStudent = new Student("Sebastian Reichard", "S1001", "Student"); |
|
|
|
|
|
|
|
@org.junit.jupiter.api.Test |
|
|
|
void writeToFile() throws IOException { |
|
|
|
Student student = sampleStudent; |
|
|
|
|
|
|
|
String filename = "testStudentData.txt"; |
|
|
|
// String filename = "testStudentData.txt"; |
|
|
|
|
|
|
|
student.writeToFile(student, filename); |
|
|
|
student.writeToFile(student, fileName()); |
|
|
|
|
|
|
|
Path path = Path.of(filename); |
|
|
|
Path path = Path.of(fileName()); |
|
|
|
String content = Files.readString(path); |
|
|
|
assertEquals("Sebastian Reichard\nS1001\nStudent", content); |
|
|
|
|
|
|
@ -28,10 +45,6 @@ class StudentTest { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
void readFromFile() throws IOException { |
|
|
|
|
|
|
@ -42,8 +55,8 @@ class StudentTest { |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
String filename = "testStudentData.txt"; |
|
|
|
List<Student> testStudents = Student.readFromFile(filename); |
|
|
|
// String filename = "testStudentData.txt"; |
|
|
|
List<Student> testStudents = Student.readFromFile(fileName()); |
|
|
|
|
|
|
|
assertEquals(1, testStudents.size()); |
|
|
|
Student admin = testStudents.get(0); |
|
|
@ -52,7 +65,7 @@ class StudentTest { |
|
|
|
assertEquals("S1001", admin.getId()); |
|
|
|
assertEquals("Student", admin.getRole()); |
|
|
|
|
|
|
|
Files.delete(Path.of(filename)); |
|
|
|
// Files.delete(Path.of(filename)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|