Skip to content

Commit fc26e40

Browse files
authored
Merge pull request #138 from Hardvan/add-coffee-tests
Add unit tests for Coffee Vending Machine
2 parents 0a2d1e7 + 684e347 commit fc26e40

3 files changed

Lines changed: 156 additions & 1 deletion

File tree

solutions/java/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>coffeevendingmachine</groupId>
6+
<artifactId>coffeevendingmachine</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>Coffee Vending Machine</name>
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
<junit.jupiter.version>5.10.2</junit.jupiter.version>
14+
</properties>
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.junit.jupiter</groupId>
18+
<artifactId>junit-jupiter</artifactId>
19+
<version>${junit.jupiter.version}</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-surefire-plugin</artifactId>
28+
<version>3.2.5</version>
29+
<configuration>
30+
<useModulePath>false</useModulePath>
31+
</configuration>
32+
</plugin>
33+
</plugins>
34+
</build>
35+
</project>

solutions/java/src/coffeevendingmachine/CoffeeVendingMachine.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ public void reset() {
6363
this.selectedCoffee = null;
6464
this.moneyInserted = 0;
6565
}
66-
}
66+
67+
public Map<String, Integer> showIngredientsMap() {
68+
return ingredientStore.getAllIngredients();
69+
}
70+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)