|
6 | 6 |
|
7 | 7 | import com.codedifferently.lesson16.Lunch.InvalidCalorieException;
|
8 | 8 | import com.codedifferently.lesson16.Lunch.Lunch;
|
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.PrintStream; |
9 | 11 | import java.util.ArrayList;
|
10 | 12 | import org.junit.jupiter.api.BeforeEach;
|
11 | 13 | import org.junit.jupiter.api.Test;
|
12 | 14 |
|
13 | 15 | public class LunchTest {
|
14 | 16 |
|
15 | 17 | private Lunch lunch;
|
| 18 | + private final PrintStream originalOut = System.out; // Store the original System.out |
16 | 19 |
|
17 | 20 | @BeforeEach
|
18 | 21 | public void setUp() throws InvalidCalorieException {
|
@@ -48,10 +51,20 @@ public void testGetHealthMessageUnhealthy() throws InvalidCalorieException {
|
48 | 51 |
|
49 | 52 | @Test
|
50 | 53 | public void testDisplayDrinks() {
|
| 54 | + // Redirect output to capture printed text |
| 55 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 56 | + System.setOut(new PrintStream(outputStream)); |
| 57 | + |
51 | 58 | lunch.addDrink("Water");
|
52 |
| - // Capture output or verify by using a mocked System.out or similar approach |
53 |
| - // For simplicity, you could check if a drink was added and assume display works |
54 |
| - assertTrue(lunch.getDrinks().contains("Water")); |
| 59 | + lunch.addDrink("Soda"); |
| 60 | + lunch.displayDrinks(); |
| 61 | + |
| 62 | + // Reset System.out to original |
| 63 | + System.setOut(originalOut); |
| 64 | + |
| 65 | + // Prepare the expected output |
| 66 | + String expectedOutput = "Available drinks:\n- Water\n- Soda\n"; |
| 67 | + assertEquals(expectedOutput, outputStream.toString()); |
55 | 68 | }
|
56 | 69 |
|
57 | 70 | // Helper method to access the drinks list for testing
|
|
0 commit comments