Browse Source
Merge branch 'fdai7887' into 'master'
Merge branch 'fdai7887' into 'master'
Fdai7887 See merge request fdai7600/PWN_Alpha_Dogs!4remotes/origin/fdai7780
fdai7887
11 months ago
3 changed files with 114 additions and 0 deletions
-
37src/main/java/org/example/Student.java
-
77src/test/java/org/example/StudentTest.java
-
0studentData.txt
@ -0,0 +1,77 @@ |
|||
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 StudentTest { |
|||
|
|||
@org.junit.jupiter.api.Test |
|||
void writeToFile() throws IOException { |
|||
Student student = new Student("Sebastian", "S1001", "Student") ; |
|||
|
|||
String filename = "testStudentData.txt"; |
|||
|
|||
student.writeToFile(student, filename); |
|||
|
|||
Path path = Path.of(filename); |
|||
String content = Files.readString(path); |
|||
assertEquals("Sebastian\nS1001\nStudent", content); |
|||
|
|||
Files.delete(path); |
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
@Test |
|||
void readFromFile() throws IOException { |
|||
|
|||
try (PrintWriter writer = new PrintWriter(new FileWriter("testStudentData.txt"))){ |
|||
writer.println("Samuel"); |
|||
writer.println("S1001"); |
|||
writer.print("Student"); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
String filename = "testStudentData.txt"; |
|||
List<Student> testStudents = Student.readFromFile(filename); |
|||
|
|||
assertEquals(1, testStudents.size()); |
|||
Student admin = testStudents.get(0); |
|||
|
|||
assertEquals("Samuel", admin.getName()); |
|||
assertEquals("S1001", admin.getId()); |
|||
assertEquals("Student", admin.getRole()); |
|||
|
|||
Files.delete(Path.of(filename)); |
|||
|
|||
} |
|||
|
|||
|
|||
@Test |
|||
void printStudentInfo() { |
|||
Student student = new Student("Sebastian", "S1001", "Student"); |
|||
|
|||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|||
System.setOut(new PrintStream(outputStream)); |
|||
|
|||
student.printStudentInfo(); |
|||
|
|||
String printedOutput = outputStream.toString().trim(); |
|||
|
|||
assertTrue(printedOutput.contains("Sebastian")); |
|||
assertTrue(printedOutput.contains("S1001")); |
|||
assertTrue(printedOutput.contains("Student")); |
|||
|
|||
} |
|||
|
|||
|
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue