|
| 1 | +package com.codedifferently.lesson16.lunch; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.junit.jupiter.api.Assertions.fail; |
| 6 | + |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.PrintStream; |
| 9 | +import java.util.ArrayList; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +public class LunchTest { |
| 14 | + |
| 15 | + private Lunch lunch; |
| 16 | + private final PrintStream originalOut = System.out; // Store the original System.out |
| 17 | + |
| 18 | + @BeforeEach |
| 19 | + public void setUp() throws InvalidCalorieException { |
| 20 | + lunch = new Lunch("Grilled Chicken", "Caesar Salad", 450, Lunch.LunchType.NON_VEGETARIAN); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testAddDrink() { |
| 25 | + lunch.addDrink("Iced Tea"); |
| 26 | + lunch.addDrink("Lemonade"); |
| 27 | + |
| 28 | + // Verify that drinks were added correctly |
| 29 | + ArrayList<String> expectedDrinks = new ArrayList<>(); |
| 30 | + expectedDrinks.add("Iced Tea"); |
| 31 | + expectedDrinks.add("Lemonade"); |
| 32 | + |
| 33 | + // Use reflection or a method to get the drinks if you have one |
| 34 | + assertEquals(expectedDrinks.size(), lunch.getDrinks().size()); |
| 35 | + assertTrue(lunch.getDrinks().containsAll(expectedDrinks)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testGetHealthMessageHealthy() throws InvalidCalorieException { |
| 40 | + lunch = new Lunch("Salad", "Fruit", 300, Lunch.LunchType.VEGAN); |
| 41 | + assertEquals("This lunch is healthy!", lunch.getHealthMessage()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testGetHealthMessageUnhealthy() throws InvalidCalorieException { |
| 46 | + lunch = new Lunch("Cheeseburger", "Fries", 800, Lunch.LunchType.NON_VEGETARIAN); |
| 47 | + assertEquals("This lunch is high in calories.", lunch.getHealthMessage()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testDisplayDrinks() { |
| 52 | + // Redirect output to capture printed text |
| 53 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 54 | + System.setOut(new PrintStream(outputStream)); |
| 55 | + |
| 56 | + lunch.addDrink("Water"); |
| 57 | + lunch.addDrink("Soda"); |
| 58 | + lunch.displayDrinks(); |
| 59 | + |
| 60 | + // Reset System.out to original |
| 61 | + System.setOut(originalOut); |
| 62 | + |
| 63 | + // Prepare the expected output |
| 64 | + String expectedOutput = "Available drinks:\n- Water\n- Soda\n"; |
| 65 | + assertEquals(expectedOutput, outputStream.toString()); |
| 66 | + } |
| 67 | + |
| 68 | + // Helper method to access the drinks list for testing |
| 69 | + public ArrayList<String> getDrinks() { |
| 70 | + return lunch.getDrinks(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testGetSideDish() throws InvalidCalorieException { |
| 75 | + assertEquals("Caesar Salad", lunch.getSideDish()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testLunchType() throws InvalidCalorieException { |
| 80 | + lunch = new Lunch("Tofu Stir Fry", "Brown Rice", 350, Lunch.LunchType.VEGAN); |
| 81 | + assertEquals(Lunch.LunchType.VEGAN, lunch.getLunchType()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testAddMultipleDrinks() { |
| 86 | + lunch.addDrink("Soda"); |
| 87 | + lunch.addDrink("Juice"); |
| 88 | + lunch.addDrink("Coffee"); |
| 89 | + |
| 90 | + ArrayList<String> expectedDrinks = new ArrayList<>(); |
| 91 | + expectedDrinks.add("Soda"); |
| 92 | + expectedDrinks.add("Juice"); |
| 93 | + expectedDrinks.add("Coffee"); |
| 94 | + |
| 95 | + assertEquals(expectedDrinks.size(), lunch.getDrinks().size()); |
| 96 | + assertTrue(lunch.getDrinks().containsAll(expectedDrinks)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testHighCalories() throws InvalidCalorieException { |
| 101 | + lunch = new Lunch("Double Cheeseburger", "Loaded Fries", 2000, Lunch.LunchType.NON_VEGETARIAN); |
| 102 | + assertEquals(2000, lunch.getCalories()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testEmptyMainDish() throws InvalidCalorieException { |
| 107 | + lunch = new Lunch("", "Salad", 300, Lunch.LunchType.VEGETARIAN); |
| 108 | + assertEquals("", lunch.getMainDish()); |
| 109 | + } |
| 110 | + |
| 111 | + // Expected to fail with exception message: Calories cannot be zero or negative |
| 112 | + @Test |
| 113 | + public void testZeroCaloriesThrowsException() { |
| 114 | + try { |
| 115 | + lunch = new Lunch("Grilled Chicken", "Caesar Salad", 0, Lunch.LunchType.NON_VEGETARIAN); |
| 116 | + fail("Expected InvalidCalorieException to be thrown"); |
| 117 | + } catch (InvalidCalorieException e) { |
| 118 | + assertEquals("Calories cannot be zero or negative.", e.getMessage()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Expected to fail with exception message: Calories cannot be zero or negative. |
| 123 | + @Test |
| 124 | + public void testNegativeCaloriesThrowsException() { |
| 125 | + try { |
| 126 | + lunch = new Lunch("Grilled Chicken", "Caesar Salad", -100, Lunch.LunchType.NON_VEGETARIAN); |
| 127 | + fail("Expected InvalidCalorieException to be thrown"); |
| 128 | + } catch (InvalidCalorieException e) { |
| 129 | + assertEquals("Calories cannot be zero or negative.", e.getMessage()); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments