Skip to content

Commit 74bbcfa

Browse files
author
jjcapparell
committed
chore: edited test to reach 100% test coverage
1 parent b062ca2 commit 74bbcfa

File tree

1 file changed

+16
-3
lines changed
  • lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/LunchTest

1 file changed

+16
-3
lines changed

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/LunchTest/LunchTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
import com.codedifferently.lesson16.Lunch.InvalidCalorieException;
88
import com.codedifferently.lesson16.Lunch.Lunch;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.PrintStream;
911
import java.util.ArrayList;
1012
import org.junit.jupiter.api.BeforeEach;
1113
import org.junit.jupiter.api.Test;
1214

1315
public class LunchTest {
1416

1517
private Lunch lunch;
18+
private final PrintStream originalOut = System.out; // Store the original System.out
1619

1720
@BeforeEach
1821
public void setUp() throws InvalidCalorieException {
@@ -48,10 +51,20 @@ public void testGetHealthMessageUnhealthy() throws InvalidCalorieException {
4851

4952
@Test
5053
public void testDisplayDrinks() {
54+
// Redirect output to capture printed text
55+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
56+
System.setOut(new PrintStream(outputStream));
57+
5158
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());
5568
}
5669

5770
// Helper method to access the drinks list for testing

0 commit comments

Comments
 (0)