|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.junit.jupiter.api.Assertions.fail; |
5 | 6 |
|
6 | 7 | import com.codedifferently.lesson16.Lunch.InvalidCalorieException; |
7 | 8 | import com.codedifferently.lesson16.Lunch.Lunch; |
|
12 | 13 | public class LunchTest { |
13 | 14 |
|
14 | 15 | private Lunch lunch; |
15 | | - private Lunch impossibleLunch; |
16 | 16 |
|
17 | 17 | @BeforeEach |
18 | 18 | public void setUp() throws InvalidCalorieException { |
@@ -94,17 +94,23 @@ public void testEmptyMainDish() throws InvalidCalorieException { |
94 | 94 |
|
95 | 95 | // Expected to fail with exception message: Calories cannot be zero or negative |
96 | 96 | @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 | + } |
101 | 104 | } |
102 | 105 |
|
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. |
104 | 107 | @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 | + } |
109 | 115 | } |
110 | 116 | } |
0 commit comments