Skip to content

Commit 0d55f9c

Browse files
author
jjcapparell
committed
fix: edit test to not fail with exception
1 parent 0e5546f commit 0d55f9c

File tree

1 file changed

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

1 file changed

+16
-10
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.fail;
56

67
import com.codedifferently.lesson16.Lunch.InvalidCalorieException;
78
import com.codedifferently.lesson16.Lunch.Lunch;
@@ -12,7 +13,6 @@
1213
public class LunchTest {
1314

1415
private Lunch lunch;
15-
private Lunch impossibleLunch;
1616

1717
@BeforeEach
1818
public void setUp() throws InvalidCalorieException {
@@ -94,17 +94,23 @@ public void testEmptyMainDish() throws InvalidCalorieException {
9494

9595
// Expected to fail with exception message: Calories cannot be zero or negative
9696
@Test
97-
public void testZeroCalories() throws InvalidCalorieException {
98-
lunch = new Lunch("Fruit Salad", "Yogurt", 0, Lunch.LunchType.VEGAN);
99-
assertEquals("Fruit Salad", lunch.getMainDish());
100-
assertEquals(0, lunch.getCalories());
97+
public void testZeroCaloriesThrowsException() {
98+
try {
99+
lunch = new Lunch("Grilled Chicken", "Caesar Salad", 0, Lunch.LunchType.NON_VEGETARIAN);
100+
fail("Expected InvalidCalorieException to be thrown");
101+
} catch (InvalidCalorieException e) {
102+
assertEquals("Calories cannot be zero or negative.", e.getMessage());
103+
}
101104
}
102105

103-
// Expected to fail with exception message: Calories cannot be zero or negative
106+
// Expected to fail with exception message: Calories cannot be zero or negative.
104107
@Test
105-
public void testNegativeCaloriesThrowsException() throws InvalidCalorieException {
106-
impossibleLunch =
107-
new Lunch("Grilled Chicken", "Caesar Salad", -100, Lunch.LunchType.NON_VEGETARIAN);
108-
assertEquals(-100, lunch.getCalories());
108+
public void testNegativeCaloriesThrowsException() {
109+
try {
110+
lunch = new Lunch("Grilled Chicken", "Caesar Salad", -100, Lunch.LunchType.NON_VEGETARIAN);
111+
fail("Expected InvalidCalorieException to be thrown");
112+
} catch (InvalidCalorieException e) {
113+
assertEquals("Calories cannot be zero or negative.", e.getMessage());
114+
}
109115
}
110116
}

0 commit comments

Comments
 (0)