From 96c6a8da43429c9e864f1acb59a1b841a37fa88c Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 22:58:10 +0100 Subject: [PATCH] test: printCourseInfo method in Course class --- src/test/java/org/example/CourseTest.java | 37 ++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 1c404db..5445a3d 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -3,7 +3,42 @@ package org.example; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import static org.junit.Assert.assertEquals; class CourseTest { - + + @Test + void printCourseInfo() { + // Create a new Course object + Course course = new Course("Introduction to Programming", "CS101", 3); + + // Set up the expected output + String expectedCourseName = "Course Name: Introduction to Programming"; + String expectedCourseCode = "Course Code: CS101"; + String expectedCourseCredits = "Course Credits: 3"; + + // Redirect System.out to capture printed output + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream originalOut = System.out; + System.setOut(new PrintStream(outputStream)); + + // Call the method to be tested + course.printCourseInfo(); + + // Reset System.out + System.setOut(originalOut); + + // Get the printed output + String printedOutput = outputStream.toString().trim(); // trim to remove trailing newline + + // Split the printed output into lines + String[] outputLines = printedOutput.split("\\r?\\n"); + + // Assert that each line matches the expected output + assertEquals(expectedCourseName, outputLines[0].trim()); + assertEquals(expectedCourseCode, outputLines[1].trim()); + assertEquals(expectedCourseCredits, outputLines[2].trim()); + } } \ No newline at end of file