From 533ffedbe4685355eca61f0c9cf2c6511cdc1683 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Wed, 7 Feb 2024 16:59:08 +0100 Subject: [PATCH] Test: Added testWriteToFile() to write the professor data into professorData.txt --- src/test/java/org/example/ProfessorTest.java | 30 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/example/ProfessorTest.java b/src/test/java/org/example/ProfessorTest.java index 002f461..65b4c2c 100644 --- a/src/test/java/org/example/ProfessorTest.java +++ b/src/test/java/org/example/ProfessorTest.java @@ -1,14 +1,38 @@ package org.example; +import org.example.Professor; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import java.io.IOException; +import static org.junit.jupiter.api.Assertions.*; +import java.io.*; import java.util.List; -import static org.junit.Assert.assertEquals; public class ProfessorTest { + @BeforeEach + void setup() throws IOException { + // Ensure the file is clean before each test + new PrintWriter("professorData.txt").close(); + } + + @AfterEach + void tearDown() { + // Clean up after tests + new File("professorData.txt").delete(); + } + + @Test + void testWriteToFile() throws IOException { + Professor professor = new Professor("P1001", "John Doe", "Professor"); + professor.writeToFile(); + + // Verify the file contains the expected data + try (BufferedReader reader = new BufferedReader(new FileReader("professorData.txt"))) { + assertEquals("Professor{professorID='P1001', Name='John Doe', role='Professor'}", reader.readLine()); + } + } @Test void testReadFromFile() throws IOException { Professor professor = new Professor("P1002", "Jane Doe", "Professor");