Assignment 04
Assignment 04
Explanation
Reviewing the homework for the RecipeBook, CoffeeMaker, Inventory, and Recipe classes was
my first step. Then I went over a list of duties that every class was expected to complete,
including making coffee and keeping track of supplies. After that, I wrote a number of quick
tests to make sure the system worked. I played around making recipes, putting ingredients in
the inventory, and turning on the coffee maker. If there were any differences, I carefully
recorded them so they could be fixed later. Fortunately, there were no problems during this
testing session. After finishing the tests, I evaluated the code coverage that they had produced.
To show the test execution and the associated code coverage percentage, I took screenshots.
Test cases:
/**
*
* @author Sarah Heckman
*
* Unit tests for CoffeeMaker class.
*/
public class CoffeeMakerTest extends TestCase {
//Set up for r1
r1 = new Recipe();
r1.setName("Coffee");
r1.setAmtChocolate("0");
r1.setAmtCoffee("3");
r1.setAmtMilk("1");
r1.setAmtSugar("1");
r1.setPrice("50");
//Set up for r2
r2 = new Recipe();
r2.setName("Mocha");
r2.setAmtChocolate("20");
r2.setAmtCoffee("3");
r2.setAmtMilk("1");
r2.setAmtSugar("1");
r2.setPrice("75");
//Set up for r3
r3 = new Recipe();
r3.setName("Latte")
;
r3.setAmtChocolate("0");
r3.setAmtCoffee("3");
r3.setAmtMilk("3");
r3.setAmtSugar("1");
r3.setPrice("100");
//Set up for r4
r4 = new Recipe();
r4.setName("Hot Chocolate");
r4.setAmtChocolate("4");
r4.setAmtCoffee("0");
r4.setAmtMilk("1");
r4.setAmtSugar("1");
r4.setPrice("65");
super.setUp();
}
/**
* Sets up the test fixture.
* (Called before every test case method.)
*/
public void setUp() { this.inv =
new Inventory();
this.r = new Recipe(); try
{
this.r.setAmtChocolate("10");
this.r.setAmtCoffee("10"); this.r.setAmtMilk("10");
this.r.setAmtSugar("10"); this.r.setName("Recipe
#1"); this.r.setPrice("20");
} catch (RecipeException e) {
// TODO Auto-generated catch block
}
/**
* Tears down the test fixture.
* (Called after every test case method.)
*/
public void tearDown() {
this.inv = null;
this.r = null;
}
/**
* Test cases for all of the setters for each of the four * different ingredients.
*/
public void testSetCoffee() { this.inv.setCoffee(10);
assertTrue(this.inv.getCoffee() == 10);
/**
* Test cases for all of the 'adder' methods for all four of
* the different ingredients.
*/
public void testAddCoffee() {
try {
this.inv.addCoffee("5");
assertTrue(this.inv.getCoffee() == 20);
} catch (InventoryException e) {
fail("Inventory Exception should not be thrown");
}
try {
this.inv.addCoffee("NO");
fail("Expected Inventory Exception to be thrown");
} catch (InventoryException e) {
assertEquals("Units of coffee must be a positive integer",
e.getMessage());
}
try {
this.inv.addCoffee("-5");
fail("Expected Inventory Exception to be thrown");
} catch (InventoryException e) {
assertEquals("Units of coffee must be a positive integer",
e.getMessage());
}
}
try {
this.inv.addMilk("NO");
fail("Expected Inventory Exception to be thrown");
} catch (InventoryException e) {
assertEquals("Units of milk must be a positive integer",
e.getMessage());
}
try {
this.inv.addMilk("-5");
fail("Expected Inventory Exception to be thrown");
} catch (InventoryException e) {
assertEquals("Units of milk must be a positive integer",
e.getMessage());
}
}
try {
this.inv.addSugar("NO");
fail("Expected Inventory Exception to be thrown");
} catch (InventoryException e) {
assertEquals("Units of sugar must be a positive integer",
e.getMessage());
}
try {
this.inv.addSugar("-5");
assertTrue(this.inv.getSugar() == 20);
} catch (InventoryException e) {
assertEquals("Units of sugar must be a positive integer",
e.getMessage());
}
}
try {
this.inv.addChocolate("NO");
fail("Expected Inventory Exception to be thrown");
} catch (InventoryException e) {
assertEquals("Units of chocolate must be a positive integer",
e.getMessage());
}
try {
this.inv.addChocolate("-5");
assertTrue(this.inv.getChocolate() == 20);
} catch (InventoryException e) {
assertEquals("Units of chocolate must be a positive integer",
e.getMessage());
}
}
/**
* Testing enoughIngredients, useIngredients, and the toString
* methods below
*/
try {
this.r.setAmtChocolate("100");
this.r.setAmtCoffee("100"); this.r.setAmtMilk("100");
this.r.setAmtSugar("100");
} catch (RecipeException e) {
}
assertTrue(!this.inv.enoughIngredients(this.r));
try {
this.r.setAmtMilk("10");
this.r.setAmtSugar("10");
} catch (RecipeException e) {
// TODO Auto-generated catch block
}
assertTrue(this.inv.enoughIngredients(this.r) == false);
}
public void testUseIngredients(){ boolean t =
this.inv.useIngredients(this.r);
assertTrue(t);
t = this.inv.useIngredients(this.r);
assertFalse(t);
}
import junit.framework.TestCase;
/**
* Sets up the test fixture.
* (Called before every test case method.)
*/
public void setUp() { this.rb = new
RecipeBook();
/**
* Testing addRecipes() below
*/
assertFalse(this.rb.addRecipe(this.r1));
assertFalse(this.rb.addRecipe(new Recipe())); }
/**
* Testing deleteRecipe() below
*/
public void testDeleteRecipe() {
this.testAddRecipes(); this.r1.setName("r1");
this.r4.setName("r4");
assertEquals(this.rb.deleteRecipe(0), this.r1.getName());
assertEquals(this.rb.deleteRecipe(3), this.r4.getName());
assertTrue(this.rb.deleteRecipe(10) == null);
assertTrue(this.rb.deleteRecipe(4) == null);
}
/**
* Testing editRecipe() below
*/
private Recipe r;
/**
* Sets up the test fixture.
* (Called before every test case method.)
*/
public void setUp() {
this.r = new Recipe();
}
/**
* Tears down the test fixture.
* (Called after every test case method.)
*/
public void tearDown() {
this.r = null;
}
/**
* Testing getters and setters for all ingredients * , price, name and toString()
for recipes.
*/
} catch (RecipeException e) {
fail("Recipe Exception should not be thrown");
}
try {
this.r.setAmtChocolate("NO");
fail("Recipe Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Units of chocolate must be a positive integer",
e.getMessage());
}
try {
this.r.setAmtChocolate("-10");
fail("Recipe Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Units of chocolate must be a positive integer",
e.getMessage());
}
try {
this.r.setAmtCoffee("-10");
fail("Recipe Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Units of coffee must be a positive integer",
e.getMessage());
}
}
try {
this.r.setAmtSugar("NO"); fail("Recipe
Exception should be thrown");
} catch (RecipeException e) { assertEquals("Units of sugar
must be a positive integer",
e.getMessage());
}
try {
this.r.setAmtSugar("-10");
fail("Recipe Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Units of sugar must be a positive integer",
e.getMessage());
}
}
try {
this.r.setAmtMilk("NO");
fail("Recipe Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Units of milk must be a positive integer",
e.getMessage());
}
try { this.r.setAmtMilk("-10");
fail("Recipe
Exception should be
thrown");
} catch (RecipeException e) {
assertEquals("Units of milk must be a positive integer",
e.getMessage());
}
}
try {
this.r.setPrice("NO");
fail("Recipe Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Price must be a positive integer",
e.getMessage());
}
try {
this.r.setPrice("-10");
fail("Recipe Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Price must be a positive integer",
e.getMessage());
}
}
/**
* Testing hashCode() and equals() methods below
*/
assertFalse(this.r.hashCode() == r2.hashCode());
r2.setName("r1");
assertTrue(this.r.hashCode() == r2.hashCode());
}
this.r.setName("r1");
assertTrue(this.r.equals(this.r));
assertFalse(this.r.equals(null));
assertFalse(this.r.equals(new Inventory()));
assertFalse(this.r.equals(r2));
Screenshots of JUnit:
Screenshots of EclEmma: