Browse Source

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

remotes/origin/fdai7780
Tobias Herbert 11 months ago
parent
commit
da57a3233f
  1. 35
      src/test/java/org/example/AdminTest.java

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

@ -1,5 +1,7 @@
package org.example; package org.example;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.*; import java.io.*;
@ -10,6 +12,23 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class AdminTest { class AdminTest {
@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()
{
String filename = "testAdminData.txt";
return filename;
}
//Tests printAdminInfo Method //Tests printAdminInfo Method
@org.junit.jupiter.api.Test @org.junit.jupiter.api.Test
void printAdminInfo() { void printAdminInfo() {
@ -31,17 +50,13 @@ class AdminTest {
@Test @Test
void writeToFile() throws IOException { void writeToFile() throws IOException {
Admin admin = new Admin("Simon", "A1001", "Admin") ; Admin admin = new Admin("Simon", "A1001", "Admin") ;
admin.writeToFile(admin, fileName());
String filename = "testAdminData.txt";
admin.writeToFile(admin, filename);
Path path = Path.of(filename);
Path path = Path.of(fileName());
String content = Files.readString(path); String content = Files.readString(path);
assertEquals("Simon\nA1001\nAdmin", content); assertEquals("Simon\nA1001\nAdmin", content);
Files.delete(path);
} }
//Tests readFromFile Method //Tests readFromFile Method
@Test @Test
@ -54,8 +69,8 @@ class AdminTest {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
String filename = "testAdminData.txt";
List<Admin> testAdmins = Admin.readFromFile(filename);
List<Admin> testAdmins = Admin.readFromFile(fileName());
assertEquals(1, testAdmins.size()); assertEquals(1, testAdmins.size());
Admin admin = testAdmins.get(0); Admin admin = testAdmins.get(0);
@ -64,7 +79,5 @@ class AdminTest {
assertEquals("A1001", admin.getId()); assertEquals("A1001", admin.getId());
assertEquals("Admin", admin.getRole()); assertEquals("Admin", admin.getRole());
Files.delete(Path.of(filename));
} }
} }
Loading…
Cancel
Save