|
| 1 | +package coffeevendingmachine; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | + |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.PrintStream; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +public class CoffeeVendingMachineTest { |
| 14 | + |
| 15 | + private CoffeeVendingMachine machine; |
| 16 | + private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 17 | + private final PrintStream originalOut = System.out; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + void setUp() { |
| 21 | + machine = CoffeeVendingMachine.getInstance(); |
| 22 | + System.setOut(new PrintStream(outputStream)); |
| 23 | + |
| 24 | + // Reset and refill ingredients for each test |
| 25 | + machine.refillIngredient("Water", 150); |
| 26 | + machine.refillIngredient("Coffee", 100); |
| 27 | + machine.refillIngredient("Milk", 100); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + @DisplayName("Test coffee selection") |
| 32 | + void testSelectCoffee() { |
| 33 | + CoffeeRecipe espresso = machine.selectCoffee("Espresso"); |
| 34 | + assertEquals("Espresso", espresso.getName()); |
| 35 | + assertEquals(2.5, espresso.getPrice()); |
| 36 | + assertEquals(50, espresso.getRecipe().get("Water")); |
| 37 | + assertEquals(20, espresso.getRecipe().get("Coffee")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + @DisplayName("Test invalid coffee selection") |
| 42 | + void testInvalidCoffeeSelection() { |
| 43 | + Exception exception = assertThrows(RuntimeException.class, () -> { |
| 44 | + machine.selectCoffee("MochaCoffee"); |
| 45 | + }); |
| 46 | + |
| 47 | + String expectedMessage = "Invalid coffee recipe: MochaCoffee"; |
| 48 | + String actualMessage = exception.getMessage(); |
| 49 | + |
| 50 | + assertTrue(actualMessage.contains(expectedMessage)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("Test successful coffee dispensing") |
| 55 | + void testDispenseCoffee() { |
| 56 | + CoffeeRecipe latte = machine.selectCoffee("Latte"); |
| 57 | + machine.dispenseCoffee(latte, new Payment(4.0)); |
| 58 | + |
| 59 | + String output = outputStream.toString(); |
| 60 | + assertTrue(output.contains("Dispensing: Latte")); |
| 61 | + assertTrue(output.contains("Processing Payment")); |
| 62 | + assertTrue(output.contains("Please collect your change: $1.0")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @DisplayName("Test insufficient payment") |
| 67 | + void testInsufficientPayment() { |
| 68 | + CoffeeRecipe cappuccino = machine.selectCoffee("Cappuccino"); |
| 69 | + |
| 70 | + Exception exception = assertThrows(RuntimeException.class, () -> { |
| 71 | + machine.dispenseCoffee(cappuccino, new Payment(2.0)); |
| 72 | + }); |
| 73 | + |
| 74 | + String expectedMessage = "Insufficient payment for Cappuccino"; |
| 75 | + String actualMessage = exception.getMessage(); |
| 76 | + |
| 77 | + assertTrue(actualMessage.contains(expectedMessage)); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("Test ingredient consumption") |
| 84 | + void testIngredientConsumption() { |
| 85 | + CoffeeRecipe latte = machine.selectCoffee("Latte"); |
| 86 | + |
| 87 | + // Check initial levels |
| 88 | + Map<String, Integer> beforeLevels = machine.showIngredientsMap(); |
| 89 | + int initialWaterLevel = beforeLevels.get("Water"); |
| 90 | + int initialCoffeeLevel = beforeLevels.get("Coffee"); |
| 91 | + int initialMilkLevel = beforeLevels.get("Milk"); |
| 92 | + |
| 93 | + machine.dispenseCoffee(latte, new Payment(3.0)); |
| 94 | + |
| 95 | + // Check levels after dispensing |
| 96 | + Map<String, Integer> afterLevels = machine.showIngredientsMap(); |
| 97 | + assertEquals(initialWaterLevel - 50, afterLevels.get("Water").intValue()); |
| 98 | + assertEquals(initialCoffeeLevel - 20, afterLevels.get("Coffee").intValue()); |
| 99 | + assertEquals(initialMilkLevel - 30, afterLevels.get("Milk").intValue()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + @DisplayName("Test ingredient refill") |
| 104 | + void testIngredientRefill() { |
| 105 | + // First check initial level |
| 106 | + Map<String, Integer> beforeLevels = machine.showIngredientsMap(); |
| 107 | + int initialWaterLevel = beforeLevels.get("Water"); |
| 108 | + |
| 109 | + // Add more water |
| 110 | + machine.refillIngredient("Water", 50); |
| 111 | + |
| 112 | + // Check after refill |
| 113 | + Map<String, Integer> afterLevels = machine.showIngredientsMap(); |
| 114 | + assertEquals(initialWaterLevel + 50, afterLevels.get("Water").intValue()); |
| 115 | + } |
| 116 | +} |
0 commit comments