Skip to content
Snippets Groups Projects
Commit 59d9ae29 authored by HSoreide's avatar HSoreide
Browse files

Write unit tests for the Ingredient class

parent e1641e46
No related branches found
No related tags found
1 merge request!7Create food and recipe classes with tests
package no.ntnu.idatt1002.demo.data.recipes;
import java.util.Objects;
/**
* The Ingredient class represents an ingredient that can be part of a recipe in real life and/or
* be available to the user in real life. When the ingredient is part of a recipe, the subclass called
......@@ -24,7 +26,7 @@ public class Ingredient {
* @param unit The unit of measure of the given amount of the ingredient.
*/
public Ingredient(FoodItem ingredient, double amount, MeasuringUnit unit) {
if(ingredient == null | amount < 0.0f | unit == null) {
if(ingredient == null | amount <= 0.0f | unit == null) {
throw new IllegalArgumentException("The ingredient must have a type, amount and measuring unit.");
}
this.foodType = ingredient;
......@@ -100,4 +102,22 @@ public class Ingredient {
'}';
}
/**
* The method checks if a given object is equal to the ingredient object.
* If the object is of the same class as well as having the same value for each class field,
* the object is equal to the Ingredient.
* @param o An Object to which the Ingredient should be compared.
* @return True if the object is equivalent to the Ingredient, false otherwise.
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Ingredient that)) return false;
return Double.compare(that.amount, amount) == 0 && foodType == that.foodType && unit == that.unit;
}
/* @Override
public int hashCode() {
return Objects.hash(foodType, amount, unit);
}*/
}
......@@ -7,7 +7,8 @@ public enum MeasuringUnit {
TSP("tsp."),
TBS("tbs."),
GR("gr."),
KG("kg.");
KG("kg."),
PC("pieces");
public final String label;
......
package no.ntnu.idatt1002.demo.data.recipes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class IngredientTest {
Ingredient testIngredient;
@BeforeEach
void beforeEach() {
testIngredient = new Ingredient(FoodItem.ONION, 0.5f, MeasuringUnit.KG);
}
@Test
@DisplayName("The constructor creates an ingredient object successfully")
void constructValidIngredient() {
Ingredient validIngredient = new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG);
assertEquals(validIngredient, new Ingredient(FoodItem.ONION, 1, MeasuringUnit.KG));
}
@Test
@DisplayName("The constructor throws exceptions for illegal input.")
void constructorThrowsExceptionsWhenItShould() {
assertThrows(IllegalArgumentException.class, () -> new Ingredient(null, 2, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, -5, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 0, MeasuringUnit.DL));
assertThrows(IllegalArgumentException.class, () -> new Ingredient(FoodItem.ONION, 2, null));
}
@Test
@DisplayName("Change of food type works for valid food type.")
void setFoodTypeWorksForValidType() {
testIngredient.setFoodType(FoodItem.LEMON);
assertEquals(new Ingredient(FoodItem.LEMON, 0.5f, MeasuringUnit.KG), testIngredient);
}
@Test
@DisplayName("Change of food type to invalid type throws exception.")
void setFoodTypeThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setFoodType(null));
}
@Test
@DisplayName("Change of food amount works for valid amount.")
void setAmountWorksForValidAmount() {
testIngredient.setAmount(2.5);
assertEquals(new Ingredient(FoodItem.LEMON, 2.5f, MeasuringUnit.KG), testIngredient);
}
@Test
@DisplayName("Change of food amount to invalid amount throws exception.")
void setAmountThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(0));
assertThrows(IllegalArgumentException.class, () -> testIngredient.setAmount(-1));
}
@Test
@DisplayName("Change of measuring unit works for valid unit.")
void setUnitWorksForValidUnit() {
testIngredient.setUnit(MeasuringUnit.TBS);
assertEquals(new Ingredient(FoodItem.LEMON, 0.5f, MeasuringUnit.TBS), testIngredient);
}
@Test
@DisplayName("Change of measuring to invalid unit throws exception.")
void setUnitThrowsException() {
assertThrows(IllegalArgumentException.class, () -> testIngredient.setUnit(null));
}
//TODO: Test for equals method?
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment