Browse Source

refactoring: Added automatic creation/deletions of test.tx in StudentTest

remotes/origin/fdai7780
Tobias Herbert 11 months ago
parent
commit
1d31a53314
  1. 5
      src/test/java/org/example/AdminTest.java
  2. 33
      src/test/java/org/example/StudentTest.java

5
src/test/java/org/example/AdminTest.java

@ -25,8 +25,7 @@ class AdminTest {
public String fileName()
{
String filename = "testAdminData.txt";
return filename;
return "testAdminData.txt";
}
//Tests printAdminInfo Method
@ -50,7 +49,7 @@ class AdminTest {
@Test
void writeToFile() throws IOException {
Admin admin = new Admin("Simon", "A1001", "Admin") ;
admin.writeToFile(admin, fileName());
Path path = Path.of(fileName());

33
src/test/java/org/example/StudentTest.java

@ -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));
}

Loading…
Cancel
Save