Tobias Herbert
11 months ago
4 changed files with 129 additions and 0 deletions
-
0adminData.txt
-
14pom.xml
-
45src/main/java/org/example/Admin.java
-
70src/test/java/org/example/AdminTest.java
@ -0,0 +1,70 @@ |
|||
package org.example; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import java.io.*; |
|||
import java.nio.file.Files; |
|||
import java.nio.file.Path; |
|||
import java.util.List; |
|||
|
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
class AdminTest { |
|||
|
|||
@org.junit.jupiter.api.Test |
|||
void printAdminInfo() { |
|||
Admin admin = new Admin("Aaron", "A1001", "Admin"); |
|||
|
|||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|||
System.setOut(new PrintStream(outputStream)); |
|||
|
|||
admin.printAdminInfo(); |
|||
|
|||
String printedOutput = outputStream.toString().trim(); |
|||
|
|||
assertTrue(printedOutput.contains("Aaron")); |
|||
assertTrue(printedOutput.contains("A1001")); |
|||
assertTrue(printedOutput.contains("Admin")); |
|||
|
|||
} |
|||
|
|||
@Test |
|||
void writeToFile() throws IOException { |
|||
Admin admin = new Admin("Simon", "A1001", "Admin") ; |
|||
|
|||
String filename = "testAdminData.txt"; |
|||
|
|||
admin.writeToFile(admin, filename); |
|||
|
|||
Path path = Path.of(filename); |
|||
String content = Files.readString(path); |
|||
assertEquals("Simon\nA1001\nAdmin", content); |
|||
|
|||
Files.delete(path); |
|||
|
|||
} |
|||
|
|||
@Test |
|||
void readFromFile() throws IOException { |
|||
|
|||
try (PrintWriter writer = new PrintWriter(new FileWriter("testAdminData.txt"))){ |
|||
writer.println("Thomas"); |
|||
writer.println("A1001"); |
|||
writer.print("Admin"); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
String filename = "testAdminData.txt"; |
|||
List<Admin> testAdmins = Admin.readFromFile(filename); |
|||
|
|||
assertEquals(1, testAdmins.size()); |
|||
Admin admin = testAdmins.get(0); |
|||
|
|||
assertEquals("Thomas", admin.getName()); |
|||
assertEquals("A1001", admin.getId()); |
|||
assertEquals("Admin", admin.getRole()); |
|||
|
|||
Files.delete(Path.of(filename)); |
|||
|
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue