From 2dbedd58790bfdea547abfd4a0653bee8038b99d Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Thu, 8 Feb 2024 22:36:31 +0100 Subject: [PATCH] test: Added testViewGradesFromStudent Function --- .../java/org/example/AdministrationTest.java | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/example/AdministrationTest.java b/src/test/java/org/example/AdministrationTest.java index f376603..0303330 100644 --- a/src/test/java/org/example/AdministrationTest.java +++ b/src/test/java/org/example/AdministrationTest.java @@ -1,7 +1,11 @@ package org.example; +import junit.framework.JUnit4TestCaseFacade; import org.junit.jupiter.api.Test; - +import java.io.InputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import java.util.List; import java.util.Map; @@ -123,7 +127,7 @@ class AdministrationTest { assertFalse(nonExistingAdminDeleted); } -@org.junit.jupiter.api.Test +@Test void testFindStudentById(){ Student student = new Student("Mark", "S1001", "Student"); @@ -447,4 +451,51 @@ class AdministrationTest { assertTrue(correct); } + + @Test + void testViewGradesFromStudent() { + Administration administration = new Administration(); + + Course course = sampleCourse; + administration.addCourse(course); + + Student stud = sampleStudent; + administration.addStudents(stud); + + administration.enrollStudentInCourse(stud.getStudentId(), course.getCourseID()); + stud.assignGrade(course,"12"); + + + String studentId = stud.getStudentId() ; + + Student student = administration.findStudentById(studentId) ; + + String printedOutput = null; + + if (student != null) { + + // Redirect System.out to capture printed output + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + + // Call the method under test + student.printGrades(); + + // Get the printed output + printedOutput = outputStream.toString().trim(); + + + } else { + System.out.println("Student with ID " + studentId + " not found."); + } + + + + assertEquals("Grades for Student ID: " + student.getStudentId() + ", Name: "+ student.getStudentName() + "\r\nCourse: Math (M101), Grade: 12", printedOutput); + + + } + + } +