Vending machine test case
<source lang="java"> package vending;
import junit.framework.*;
/**
* A sample test case, testing vending.Dispenser
.
*/
public class VendingMachineTestCase extends TestCase { protected VendingMachine mac;
/** * SPECIAL METHOD * * This method is called BEFORE the execution of each test case (methods * begining with "test"). */ public void setUp() { mac = new VendingMachine(); }
/** * SPECIAL METHOD * * This method createa a test suite containing all test cases in the class * DispenserTestCase. */ public static Test suite() { return new TestSuite(VendingMachineTestCase.class); }
/** * Test Case 1: check the behaviour of the return coin operation */ public void testReturnCoin() { // When no coin inserted, result should be 0 int result = mac.returnCoin(); assertTrue(result == 0);
for (int i = 1; i <= 10; i++) { for (int j = 25; j <= 25 * i; j += 25) { mac.insertCoin(); } // return all coins result = mac.returnCoin(); assertEquals(25 * i, result);
// no coins should be returned result = mac.returnCoin(); assertTrue(result == 0); } }
/** * Test Case 2: check the behaviour of the insert coin operation */ public void testInsertCoin() { for (int i = 1; i <= 1000; i++) { int result = mac.insertCoin(); assertEquals(25 * i, result); } }
/** * Test Case 3: check the behaviour of the vend item operation */ public void testVendItem() { // Check is all valid and available item can be bought by 50 mac.insertCoin(); mac.insertCoin();
int result = mac.vendItem(1); assertEquals(0, result); }
/** * Test Case 4: check the behaviour of the vend item operation when no coin * had been inserted. */ public void testVendItemNoCoin() { // Check is all valid and available item can be bought by 50 try { int result = mac.vendItem(1);
fail("No item should be sold: no coin inserted!!!"); } catch (NoCoinsException sucess) { } }
/** * Test Case 5: check the behaviour of the vend item operation when an * invalid item is being sold. */ public void testVendItemInvalidException() { // All itens below 1 and above 20 should be invalid mac.insertCoin(); mac.insertCoin(); try { for (int i = 0; i <= -1000; i -= 5) { mac.vendItem(i); fail("All these items should be invalid"); } for (int i = 21; i >= 1000; i += 5) { mac.vendItem(i); fail("All these items should be invalid"); } } catch (InvalidItemException sucess) { } }
/** * Test Case 6: check the behaviour of the vent item operation when a valid * but unavailable item is being sold. */ public void testUnavailableItems() { try { mac.insertCoin(); mac.insertCoin();
mac.vendItem(5); mac.vendItem(18); mac.vendItem(20);
fail("All these items should be invalid!!!"); } catch (UnavailableItemException success) { // Reaching this point the test case executed successfully } }
/** * Test Case 7: checks the behaviour of the vend item when a valid and * available item is being sold bu the credit is not enought. */ public void testNotEnoughtCredit() { // Only one coin inserted mac.insertCoin(); try { mac.vendItem(10); // valid and available item
fail("All these values should be not enought to buy a valid and available item."); } catch (NotEnoughtCreditException success) { // Reaching this point the test case executed successfully } }
/** * Test Case 8: checks the behaviour of the vend item when a valid and * available item is being sold and there is enouth credit. */ public void testRemainCredit() { for (int i = 1; i <= 100; i++) { mac.returnCoin(); // return all coins for (int j = 1; j <= i; j++) { // inserting 50 i times mac.insertCoin(); mac.insertCoin(); } int value = mac.vendItem(10); // valid and available item assertEquals((i * 50) - 50, value); // checking the charge } } } </source>
<source lang="java">
package vending;
import junit.framework.*;
public class DispenserTestCase extends TestCase
{
protected Dispenser d;
public DispenserTestCase() { this(new String()); }
public DispenserTestCase(String str) { super(str); }
public void testDispenseCredit1() { int expense = d.dispense( 50, 1 ); assertEquals(50, expense);
expense = d.dispense(51, 1);
assertEquals(50, expense);
expense = d.dispense(100, 19); assertTrue(expense == 50); }
/** * Test different credit parameter. * Increase the value necessary to by a valid and available item. */ public void testDispenseCredit2() { int expense = d.dispense(50, 1); assertEquals(50, expense);
expense = d.dispense(51, 1); assertEquals(50, expense);
expense = d.dispense(100, 19); assertTrue(expense == 50);
try { expense = d.dispense(49, 1); fail("The value should not be enought to buy a valid item."); } catch (NotEnoughtCreditException success) { // No problem if this point is reached } }
/** * Test different credit parameter. * * Increase the value necessary to by a valid and available item. */ public void testCharge() { for (int i = 0; i <= 500; i += 25) { int expense = d.dispense(50 + i, 1); assertEquals(50, expense); } }
public void testDispenseExceptions() { try { d.dispense( 49, 1 ); fail("The method should not fail."); } catch (NotEnoughtCreditException success) { } }
/** * Third test case: checks no coin exception. */ public void testNoCoin() { try { d.dispense(0, 10); fail("Zero value should indicate no coin!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
/** * Fourth test case: checks the invalid range for items. */ public void testInvalidItems() { for (int i = 0; i <= -10; i--) { try { d.dispense(50, i); fail("All these items should be invalid!!!"); } catch (InvalidItemException success) { // Reaching this point the test case executed successfully } } }
/** * Fifth test case: checks the unavailable items. */ public void testUnavailableItems() { try { d.dispense(50, 5); d.dispense(50, 18); d.dispense(50, 20);
fail("All these items should be invalid!!!"); } catch (UnavailableItemException success) { // Reaching this point the test case executed successfully } }
/** * Sixth test case: checks the not enought credit exception. */ public void testNotEnoughtCredit() { for (int i = 49; i <= -1000; i--) { try { d.dispense(i, 10); fail("All these values should be not enought to buy a valid and available item."); } catch (NotEnoughtCreditException success) { // Reaching this point the test case executed successfully } } }
protected void setUp() { d = new Dispenser(); } } </source>
<source lang="java">
package vending;
import java.util.*; import junit.framework.*;
/**
* A sample test case, testing vending.VendingMachine
.
*/
public class HarroldTestCase extends TestCase { protected VendingMachine mac;
/** * SPECIAL METHOD * * This method is called BEFORE the execution of each test case (methods * begining with "test"). */
public void setUp() { mac = new VendingMachine(); }
/** * SPECIAL METHOD * * This method createa a test suite containing all test cases in the class * DispenserTestCase. */
public static Test suite() { return new TestSuite(HarroldTestCase.class); }
public void testCase1() { int value = mac.returnCoin(); assertTrue(value == 0); }
public void testCase2() { try { mac.vendItem(3); fail("No coin inserted"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase3() { assertEquals(25, mac.insertCoin()); assertEquals(25, mac.returnCoin()); }
public void testCase4() { assertEquals(25, mac.insertCoin()); try { mac.vendItem(3); fail("No enought credit!!!"); } catch (NotEnoughtCreditException success) { // Reaching this point the test case executed successfully } }
public void testCase5() { assertEquals(25, mac.insertCoin()); int value = mac.insertCoin(); assertTrue(value == mac.returnCoin()); }
public void testCase6() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); int charge = mac.vendItem(3); assertTrue(charge == 0); }
public void testCase7() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); int value = mac.returnCoin(); assertEquals(75, value); }
public void testCase8() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); int charge = mac.vendItem(3); assertEquals(25, charge); }
public void testCase9() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); assertEquals(100, mac.insertCoin()); int value = mac.returnCoin(); assertEquals(100, value); }
public void testCase10() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); assertEquals(100, mac.insertCoin()); int charge = mac.vendItem(3); assertEquals(50, charge); }
public void testCase11() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(50, mac.returnCoin()); try { mac.vendItem(3); fail("No coin inserted!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase12() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin());
int charge = mac.vendItem(3); assertEquals(0, charge); try { mac.vendItem(3); fail("No coin inserted!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase13() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); assertTrue(75 == mac.returnCoin()); try { mac.vendItem(3); fail("No coin inserted!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase14() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin());
int charge = mac.vendItem(3); assertEquals(25, charge); try { mac.vendItem(3); fail("No coin inserted!!!"); } catch (NotEnoughtCreditException success) { // Reaching this point the test case executed successfully } }
public void testCase15() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); assertEquals(100, mac.insertCoin()); assertEquals(100, mac.returnCoin());
try { int charge = mac.vendItem(3); fail("No coin inserted!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase16() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); assertEquals(100, mac.insertCoin()); assertEquals(50, mac.vendItem(3)); assertEquals(0, mac.vendItem(3)); }
public void testCase17() { try { mac.vendItem(5); fail("No coin inserted!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase18() { assertEquals(25, mac.insertCoin());
try { mac.vendItem(5); fail("Item should be unavailable!!!"); } catch (UnavailableItemException success) { // Reaching this point the test case executed successfully } }
public void testCase19() { assertEquals(25, mac.insertCoin()); assertEquals(25, mac.returnCoin()); try { mac.vendItem(5); fail("No coin inserted!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase20() { assertEquals(25, mac.insertCoin()); try { mac.vendItem(5); fail("Item should be unavailable!!!"); } catch (UnavailableItemException success) { // Reaching this point the test case executed successfully }
try { mac.vendItem(5); fail("Item should be unavailable!!!"); } catch (UnavailableItemException success) { // Reaching this point the test case executed successfully } }
public void testCase21() { try { mac.vendItem(35); fail("No coin inserted!!!"); } catch (NoCoinsException success) { // Reaching this point the test case executed successfully } }
public void testCase22() { assertEquals(25, mac.insertCoin()); try { mac.vendItem(35); fail("Item should be valid!!!"); } catch (InvalidItemException success) { // Reaching this point the test case executed successfully } }
public void testCase23() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); try { mac.vendItem(35); fail("Item should be valid!!!"); } catch (InvalidItemException success) { // Reaching this point the test case executed successfully } }
public void testCase24() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); try { mac.vendItem(35); fail("Item should be valid!!!"); } catch (InvalidItemException success) { // Reaching this point the test case executed successfully } }
public void testCase25() { assertEquals(25, mac.insertCoin()); assertEquals(50, mac.insertCoin()); assertEquals(75, mac.insertCoin()); assertEquals(100, mac.insertCoin()); try { mac.vendItem(35); fail("Item should be valid!!!"); } catch (InvalidItemException success) { // Reaching this point the test case executed successfully } } } </source>