Skip to content

Commit 463923b

Browse files
author
jjcapparell
committed
chore: added more tests for Lunch class with new InvalidCalorieException file
1 parent e970116 commit 463923b

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.Lunch;
2+
3+
public class InvalidCalorieException extends Exception {
4+
public InvalidCalorieException(String message) {
5+
super(message);
6+
}
7+
}

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lunch/Lunch.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public enum LunchType {
1919
private final ArrayList<String> drinks; // Collection type (ArrayList)
2020

2121
// Constructor
22-
public Lunch(String mainDish, String sideDish, int calories, LunchType lunchType) {
22+
public Lunch(String mainDish, String sideDish, int calories, LunchType lunchType) throws InvalidCalorieException {
23+
if (calories <= 0) {
24+
throw new InvalidCalorieException("Calories cannot be zero or negative.");
25+
}
2326
this.mainDish = mainDish;
2427
this.sideDish = sideDish;
2528
this.calories = calories;

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

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

6+
import com.codedifferently.lesson16.Lunch.InvalidCalorieException;
67
import com.codedifferently.lesson16.Lunch.Lunch;
78
import java.util.ArrayList;
89
import org.junit.jupiter.api.BeforeEach;
@@ -11,9 +12,10 @@
1112
public class LunchTest {
1213

1314
private Lunch lunch;
15+
private Lunch impossibleLunch;
1416

1517
@BeforeEach
16-
public void setUp() {
18+
public void setUp() throws InvalidCalorieException {
1719
lunch = new Lunch("Grilled Chicken", "Caesar Salad", 450, Lunch.LunchType.NON_VEGETARIAN);
1820
}
1921

@@ -33,13 +35,13 @@ public void testAddDrink() {
3335
}
3436

3537
@Test
36-
public void testGetHealthMessageHealthy() {
38+
public void testGetHealthMessageHealthy() throws InvalidCalorieException {
3739
lunch = new Lunch("Salad", "Fruit", 300, Lunch.LunchType.VEGAN);
3840
assertEquals("This lunch is healthy!", lunch.getHealthMessage());
3941
}
4042

4143
@Test
42-
public void testGetHealthMessageUnhealthy() {
44+
public void testGetHealthMessageUnhealthy() throws InvalidCalorieException {
4345
lunch = new Lunch("Cheeseburger", "Fries", 800, Lunch.LunchType.NON_VEGETARIAN);
4446
assertEquals("This lunch is high in calories.", lunch.getHealthMessage());
4547
}
@@ -56,4 +58,52 @@ public void testDisplayDrinks() {
5658
public ArrayList<String> getDrinks() {
5759
return lunch.getDrinks();
5860
}
59-
}
61+
62+
@Test
63+
public void testLunchType() throws InvalidCalorieException {
64+
lunch = new Lunch("Tofu Stir Fry", "Brown Rice", 350, Lunch.LunchType.VEGAN);
65+
assertEquals(Lunch.LunchType.VEGAN, lunch.getLunchType());
66+
}
67+
68+
@Test
69+
public void testAddMultipleDrinks() {
70+
lunch.addDrink("Soda");
71+
lunch.addDrink("Juice");
72+
lunch.addDrink("Coffee");
73+
74+
ArrayList<String> expectedDrinks = new ArrayList<>();
75+
expectedDrinks.add("Soda");
76+
expectedDrinks.add("Juice");
77+
expectedDrinks.add("Coffee");
78+
79+
assertEquals(expectedDrinks.size(), lunch.getDrinks().size());
80+
assertTrue(lunch.getDrinks().containsAll(expectedDrinks));
81+
}
82+
83+
@Test
84+
public void testHighCalories() throws InvalidCalorieException {
85+
lunch = new Lunch("Double Cheeseburger", "Loaded Fries", 2000, Lunch.LunchType.NON_VEGETARIAN);
86+
assertEquals(2000, lunch.getCalories());
87+
}
88+
89+
@Test
90+
public void testEmptyMainDish() throws InvalidCalorieException {
91+
lunch = new Lunch("", "Salad", 300, Lunch.LunchType.VEGETARIAN);
92+
assertEquals("", lunch.getMainDish());
93+
}
94+
95+
// Expected to fail with exception message: Calories cannot be zero or negative
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());
101+
}
102+
103+
// Expected to fail with exception message: Calories cannot be zero or negative
104+
@Test
105+
public void testNegativeCaloriesThrowsException() throws InvalidCalorieException {
106+
impossibleLunch = new Lunch("Grilled Chicken", "Caesar Salad", -100, Lunch.LunchType.NON_VEGETARIAN);
107+
assertEquals(-100, lunch.getCalories());
108+
}
109+
}

0 commit comments

Comments
 (0)