0% found this document useful (0 votes)
29 views

Assignment 04

The document describes writing tests for a coffee maker application. It discusses reviewing the application code, writing tests to ensure it works as expected, and evaluating the code coverage of the tests. It then provides examples of two test cases - one for the coffee maker class and one for the inventory class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Assignment 04

The document describes writing tests for a coffee maker application. It discusses reviewing the application code, writing tests to ensure it works as expected, and evaluating the code coverage of the tests. It then provides examples of two test cases - one for the coffee maker class and one for the inventory class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Assignment 4 - JUnit and Coverage Testing

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:

Coffee Maker test case:


package edu.ncsu.csc326.coffeemaker;

import edu.ncsu.csc326.coffeemaker.exceptions.InventoryException; import


junit.framework.TestCase;

/**
*
* @author Sarah Heckman
*
* Unit tests for CoffeeMaker class.
*/
public class CoffeeMakerTest extends TestCase {

private CoffeeMaker cm;


private Recipe r1;
private Recipe r2;
private Recipe r3;
private Recipe r4;

protected void setUp() throws Exception {


cm = new CoffeeMaker();

//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();
}

public void testAddInventory() {


try {
cm.addInventory("4","7","0","9"); }
catch (InventoryException e) {
fail("InventoryException should not be thrown");
}
}

public void testAddInventoryException() {


try {
cm.addInventory("4", "-1", "asdf", "3");
fail("InventoryException should be thrown");
} catch (InventoryException e) {
//success if thrown
}
}

public void testMakeCoffee() {


cm.addRecipe(r1);
assertEquals(25, cm.makeCoffee(0, 75));
}

Inventory Test Case:


package edu.ncsu.csc326.coffeemaker;

import junit.framework.TestCase; import


edu.ncsu.csc326.coffeemaker.Inventory;
import edu.ncsu.csc326.coffeemaker.exceptions.InventoryException; import
edu.ncsu.csc326.coffeemaker.exceptions.RecipeException;

public class InventoryTest extends TestCase {

private Inventory inv;


private Recipe r;

/**
* 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);

public void testSetMilk() {


this.inv.setMilk(10);
assertTrue(this.inv.getMilk() == 10);
}

public void testSetSugar() {


this.inv.setSugar(10);
assertTrue(this.inv.getSugar() == 10);
}

public void testSetChocolate() {


this.inv.setChocolate(10);
assertTrue(this.inv.getChocolate() == 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());
}
}

public void testAddMilk() {


try {
this.inv.addMilk("5");
assertTrue(this.inv.getMilk() == 20); } catch
(InventoryException e) {
fail("Inventory Exception should not be thrown");
}

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());
}
}

public void testAddSugar() {


try {
this.inv.addSugar("5");
assertTrue(this.inv.getSugar() == 20); } catch
(InventoryException e) {
fail("Inventory Exception should not be thrown");
}

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());
}
}

public void testAddChocolate() {


try {
this.inv.addChocolate("5");
assertTrue(this.inv.getChocolate() == 20);
} catch (InventoryException e) {
fail("Inventory Exception should not be thrown");
}

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
*/

public void testEnoughIngredients(){


assertTrue(this.inv.enoughIngredients(this.r) == true);

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);

assertFalse(this.inv.getChocolate() > 15);


assertFalse(this.inv.getCoffee() > 15);
assertFalse(this.inv.getMilk() > 15); assertFalse(this.inv.getSugar() > 15);

t = this.inv.useIngredients(this.r);
assertFalse(t);
}

Recipe Book Test Case:


package edu.ncsu.csc326.coffeemaker;

import junit.framework.TestCase;

public class RecipeBookTest extends TestCase {

private RecipeBook rb;


private Recipe r1, r2, r3 , r4;

/**
* Sets up the test fixture.
* (Called before every test case method.)
*/
public void setUp() { this.rb = new
RecipeBook();

this.r1 = new Recipe(); this.r2


= new Recipe(); this.r3 = new
Recipe(); this.r4 = new
Recipe();
}
/**
* Tears down the test fixture.
* (Called after every test case method.)
*/
public void tearDown() {
this.rb = null;
this.r1 = null; this.r2 =
null; this.r3 = null;
this.r4 = null;
}

/**
* Testing addRecipes() below
*/

public void testAddRecipes() {


assertTrue(this.rb.addRecipe(this.r1));
assertTrue(this.rb.addRecipe(this.r2));
assertTrue(this.rb.addRecipe(this.r3));
assertTrue(this.rb.addRecipe(this.r4));

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
*/

public void testEditRecipe() {


this.testAddRecipes(); this.r1.setName("r1");
this.r4.setName("r4");

Recipe nr1 = new Recipe();


nr1.setName("nr1");
Recipe nr4 = new Recipe();
nr4.setName("nr4");
assertEquals(this.rb.editRe
cipe(0, nr1), r1.getName());
assertEquals(this.rb.editRe
cipe(3, nr4), r4.getName());

assertTrue(this.rb.editRecipe(10, nr4) == null);


assertTrue(this.rb.editRecipe(4, nr4) == null);
}
/**
* Testing getRecipes() below
*/

public void testGetRecipes() {


this.r1.setName("r1"); this.r2.setName("r2");
this.r3.setName("r3");
this.r4.setName("r4"); this.testAddRecipes();

for (int i = 0; i <= 3; i ++)


assertEquals(this.rb.getRecipes()[i].getName(),
("r" + (i + 1)));
}

Recipe Test Case: package


edu.ncsu.csc326.coffeemaker;

import edu.ncsu.csc326.coffeemaker.exceptions.RecipeException; import


junit.framework.TestCase;

public class RecipeTest extends TestCase {

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.
*/

public void testSetAmtChocolate() { try


{
this.r.setAmtChocolate("20");
assertTrue(this.r.getAmtChocolate() == 20);

} 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());
}

public void testSetAmtCoffee() {


try {
this.r.setAmtCoffee("20");
assertTrue(this.r.getAmtCoffee() == 20);
} catch (RecipeException e) {
fail("Recipe Exception should not be thrown");
}
try {
this.r.setAmtCoffee("NO"); fail("Recipe
Exception should be thrown");
} catch (RecipeException e) {
assertEquals("Units of coffee 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());
}
}

public void testSetAmtSugar() {


try {
this.r.setAmtSugar("20");
assertTrue(this.r.getAmtSugar() == 20);
} catch (RecipeException e) {
fail("Inventory Exception should not be thrown");
}

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());
}
}

public void testSetAmtMilk() {


try {
this.r.setAmtMilk("20");
assertTrue(this.r.getAmtMilk() == 20); } catch
(RecipeException e) {
fail("Recipe Exception should not be thrown");
}

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());
}
}

public void testSetPrice() {


try {
this.r.setPrice("10");
assertTrue(this.r.getPrice() == 10); } catch
(RecipeException e) {
fail("Recipe Exception should not be thrown");
}

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());
}
}

public void testSetName() {


String s = "Coffee with sugar";
this.r.setName(s);
assertEquals(this.r.getName(), s);
assertEquals(this.r.toString(), s);
s = "Another name";
this.r.setName(s);
assertEquals(this.r.getName(), s);
assertEquals(this.r.toString(), s);
}

/**
* Testing hashCode() and equals() methods below
*/

public void testHashCode() {


Recipe r2 = new Recipe();
r2.setName("r2");
this.r.setName("r1");

assertFalse(this.r.hashCode() == r2.hashCode());

r2.setName("r1");
assertTrue(this.r.hashCode() == r2.hashCode());
}

public void testEquals() {


Recipe r2 = new Recipe();
r2.setName("r2");

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));

this.r = new Recipe();


r2 = new Recipe();
assertFalse(this.r.equals(r2));
}
}

Screenshots of JUnit:
Screenshots of EclEmma:

You might also like