Added tests for Recipe.

This commit is contained in:
Oskar Rasieński 2025-11-21 20:17:40 +01:00 committed by Steven Liu
commit 873f1911ac

View file

@ -0,0 +1,128 @@
package commons;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class RecipeTest {
Recipe recipe;
@BeforeEach
void setupRecipe() {
this.recipe = new Recipe(1L, "Chocolate Cake");
}
@Test
void getId() {
assertEquals(1L, recipe.getId());
}
@Test
void setId() {
recipe.setId(2048L);
assertEquals(2048L, recipe.getId());
}
@Test
void getName() {
assertEquals("Chocolate Cake", recipe.getName());
}
@Test
void setName() {
recipe.setName("Steak");
assertEquals("Steak", recipe.getName());
}
@Test
void getIngredientsAddThrow() {
// TODO: Change to actual Ingredient class later
assertThrows(UnsupportedOperationException.class, () -> {
recipe.getIngredients().add("Lasagna");
});
}
@Test
void getIngredientsClearThrow() {
assertThrows(UnsupportedOperationException.class, () -> {
recipe.getIngredients().clear();
});
}
@Test
void setIngredients() {
// TODO: Change to actual Ingredient class later
List<String> ingredients = new ArrayList<>(List.of("Chocolate", "Flour", "Egg"));
recipe.setIngredients(ingredients);
assertEquals(recipe.getIngredients(), ingredients);
assertEquals(recipe.getIngredients().size(), 3);
}
@Test
void getPreparationStepsAddThrow() {
assertThrows(UnsupportedOperationException.class, () -> {
recipe.getPreparationSteps().add("Preheat Oven");
});
}
@Test
void getPreparationStepsClearThrow() {
assertThrows(UnsupportedOperationException.class, () -> {
recipe.getPreparationSteps().clear();
});
}
@Test
void setPreparationSteps() {
List<String> steps = new ArrayList<>(List.of("Preheat oven", "Mix stuff", "decorate"));
recipe.setPreparationSteps(steps);
assertEquals(recipe.getPreparationSteps(), steps);
assertEquals(recipe.getPreparationSteps().size(), 3);
}
@Test
void testEqualsSame() {
Recipe recipe2 = new Recipe(1L, "Chocolate Cake");
assertEquals(recipe, recipe2);
}
@Test
void testEqualsSameExceptId() {
Recipe recipe2 = new Recipe(2L, "Chocolate Cake");
assertNotEquals(recipe, recipe2);
}
@Test
void testEqualsOnlySameId() {
Recipe recipe2 = new Recipe(1L, "Some random recipe");
assertEquals(recipe, recipe2); // Equals, we only look at ID!!!
}
@Test
void testHashCodeSame() {
Recipe recipe2 = new Recipe(1L, "Chocolate Cake");
assertEquals(recipe.hashCode(), recipe2.hashCode());
}
@Test
void testHashCodeSameExceptId() {
Recipe recipe2 = new Recipe(2L, "Chocolate Cake");
assertNotEquals(recipe.hashCode(), recipe2.hashCode());
}
@Test
void testHashCodeOnlySameId() {
Recipe recipe2 = new Recipe(1L, "Some random recipe");
assertEquals(recipe.hashCode(), recipe2.hashCode()); // Same, only looks at ID!!!
}
}