|
@ -3,7 +3,42 @@ package org.example; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
|
|
import java.io.PrintStream; |
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
|
|
|
|
class CourseTest { |
|
|
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()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |