Coffeemaker Jacoco
Coffeemaker Jacoco
coffeemaker;
import org.junit.Before;
import org.junit.Test;
import edu.ncsu.csc326.coffeemaker.exceptions.InventoryException;
import edu.ncsu.csc326.coffeemaker.exceptions.RecipeException;
/**
* Unit tests for CoffeeMaker class.
*
* @author Sarah Heckman
*/
public class CoffeeMakerTest
{
/**
* The object under test.
*/
private CoffeeMaker coffeeMaker;
/**
* Initializes some recipes to test with and the {@link CoffeeMaker}
* object we wish to test.
*
* @throws RecipeException if there was an error parsing the ingredient
* amount when setting up the recipe.
*/
@Before
public void setUp() throws RecipeException
{
coffeeMaker = new CoffeeMaker();
//Set up for r1
recipe1 = new Recipe();
recipe1.setName("Coffee");
recipe1.setAmtChocolate("0");
recipe1.setAmtCoffee("3");
recipe1.setAmtMilk("1");
recipe1.setAmtSugar("1");
recipe1.setPrice("50");
//Set up for r2
recipe2 = new Recipe();
recipe2.setName("Mocha");
recipe2.setAmtChocolate("20");
recipe2.setAmtCoffee("3");
recipe2.setAmtMilk("1");
recipe2.setAmtSugar("1");
recipe2.setPrice("75");
//Set up for r3
recipe3 = new Recipe();
recipe3.setName("Latte");
recipe3.setAmtChocolate("0");
recipe3.setAmtCoffee("3");
recipe3.setAmtMilk("3");
recipe3.setAmtSugar("1");
recipe3.setPrice("100");
//Set up for r4
recipe4 = new Recipe();
recipe4.setName("Hot Chocolate");
recipe4.setAmtChocolate("4");
recipe4.setAmtCoffee("0");
recipe4.setAmtMilk("1");
recipe4.setAmtSugar("1");
recipe4.setPrice("65");
/**
* Given a coffee maker with the default inventory
* When we add inventory with well-formed quantities
* Then we do not get an exception trying to read the inventory quantities.
*
* @throws InventoryException if there was an error parsing the quanity
* to a positive integer.
*/
@Test
public void testAddInventory() throws InventoryException
{
coffeeMaker.addInventory("4","7","0","9");
}
/**
* Given a coffee maker with the default inventory
* When we add inventory with malformed quantities (i.e., a negative
* quantity and a non-numeric string)
* Then we get an inventory exception
*
* @throws InventoryException if there was an error parsing the quanity
* to a positive integer.
*/
@Test(expected = InventoryException.class)
public void testAddInventoryException() throws InventoryException
{
coffeeMaker.addInventory("4", "-1", "asdf", "3");
}
/**
* Given a coffee maker with one valid recipe
* When we make coffee, selecting the valid recipe and paying more than
* the coffee costs
* Then we get the correct change back.
*/
@Test
public void testMakeCoffee()
{
coffeeMaker.addRecipe(recipe1);
assertEquals(25, coffeeMaker.makeCoffee(0, 75));
}
/**
* A recipe may be deleted from the CoffeeMaker if it exists in the list of
recipes in the CoffeeMaker. - ??
*/
@Test
public void testDeleteRecipe()
{
coffeeMaker.addRecipe(recipe1);
coffeeMaker.addRecipe(recipe2);
coffeeMaker.deleteRecipe(0);
Recipe [] array = coffeeMaker.getRecipes();
assertEquals(null, array[0]);
}
/**
* Only three recipes may be added to the CoffeeMaker - DONE!
*/
@Test
public void testAddRecipe()
{
coffeeMaker.addRecipe(recipe1);
coffeeMaker.addRecipe(recipe2);
coffeeMaker.addRecipe(recipe3);
boolean result = coffeeMaker.addRecipe(recipe4);
assertEquals(false, result);
}
/**
* Inventory may only be removed from the CoffeeMaker by purchasing a
beverage - DONE!
*/
@Test
public void testInventoryRemoval()
{
coffeeMaker.addRecipe(recipe1);
coffeeMaker.makeCoffee(0,50);
assertEquals("Coffee: " + "12\n" + "Milk: " + "14\n" + "Sugar: " + "14\
n" + "Chocolate: " + "15\n", coffeeMaker.checkInventory());
}
/**
* Inventory may be checked at any time from the main menu. The units of each
item in the inventory are displayed.
*/
@Test
public void testCheckInventory()
{
assertEquals("Coffee: " + "15\n" + "Milk: " + "15\n" + "Sugar: " + "15\
n" + "Chocolate: " + "15\n", coffeeMaker.checkInventory());
}
/**
* A recipe may be edited in the CoffeeMaker if it exists in the list of
recipes in the CoffeeMaker. A recipe name may not be changed.
*/
@Test
public void testEditRecipe() throws RecipeException
{
coffeeMaker.addRecipe(recipe1);
coffeeMaker.addRecipe(recipe2);
String recipeName = coffeeMaker.editRecipe(0, recipe3);
assertEquals("Coffee", recipe3.getName());
}
}