Skip to content
Snippets Groups Projects
Commit 716fdaf3 authored by HSoreide's avatar HSoreide
Browse files

Write unit tests for the RecipeRegister class

parent 84be21c4
No related branches found
No related tags found
1 merge request!7Create food and recipe classes with tests
Pipeline #204966 failed
......@@ -5,18 +5,27 @@ import java.util.List;
public class RecipeRegister {
private List<Recipe> recipes = new ArrayList<>();
private final List<Recipe> recipes = new ArrayList<>();
//TODO: Copy-constructor
public List<Recipe> getRecipes() {
return recipes;
}
public void addRecipe (Recipe recipe) {
if(recipes.contains(recipe)) {
throw new IllegalArgumentException("The recipe already exists in the register."); // Or just replace?
} else if (recipe == null) {
throw new IllegalArgumentException("The recipe cannot be null.");
throw new NullPointerException("The recipe cannot be null.");
}
this.recipes.add(recipe);
}
public Recipe getRecipe(String name) {
return recipes.stream().
filter((recipe) -> recipe.getName().equals(name))
.findFirst().orElse(null);
}
}
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 RecipeRegisterTest {
RecipeRegister myRecipes;
@BeforeEach
void beforeEach() {
myRecipes = new RecipeRegister();
myRecipes.addRecipe(new Recipe("Pasta al Limone", "Instructions"));
}
@Test
@DisplayName("Adding recipe to register successfully.")
void addRecipeToRegister() {
int numberOfRecipes = myRecipes.getRecipes().size();
myRecipes.addRecipe(new Recipe("Carbonara", "Instructions"));
assertEquals(numberOfRecipes + 1, myRecipes.getRecipes().size());
}
@Test
@DisplayName("Throw exception when adding already registered recipe.")
void recipeAlreadyInRegister() {
assertThrows(IllegalArgumentException.class, () -> myRecipes.addRecipe( new Recipe("Pasta al Limone", "Instructions")));
}
@Test
@DisplayName("Throw exception when adding null recipe.")
void addNullRecipe() {
assertThrows(NullPointerException.class, () -> myRecipes.addRecipe(null));
}
@Test
@DisplayName("Returns correct recipe based on name.")
void getRecipeByName() {
assertEquals(new Recipe("Pasta al Limone", "Instructions"), myRecipes.getRecipe("Pasta al Limone"));
}
}
\ 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