Download code
From LiteratePrograms
Back to Testing_Vending_Machine_(Java,_Junit)
Download for Windows: single file, zip
Download for UNIX: single file, zip, tar.gz, tar.bz2
TestVendingMachine.java
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Testing_Vending_Machine_(Java,_Junit)?action=history&offset=20070527171906 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Testing_Vending_Machine_(Java,_Junit)?oldid=10378 25 */ 26 27 import java.util.Random; 28 29 30 /** 31 * The test class TestVendingMachine. 32 * 33 * @author LiteratePrograms.org 34 * @version May 2007. 35 */ 36 public class TestVendingMachine extends junit.framework.TestCase 37 { 38 private VendingMachine sodaVendor; 39 private VendingMachine candyBarVendor; 40 41 private static final int SODA_PRICE = 75; 42 private static final int CANDY_BAR_PRICE = 60; 43 44 private static int 45 ONE_P = 1, 46 TWO_P = 2, 47 FIVE_P = 5, 48 TEN_P = 10, 49 TWENTY_P = 20, 50 FIFTY_P = 50, 51 ONE_POUND = 100, 52 TWO_POUNDS = 200; 53 54 private int[] coinage = {ONE_P, TWO_P, FIVE_P, TEN_P, TWENTY_P, FIFTY_P, ONE_POUND, TWO_POUNDS}; 55 56 Random generator = new Random(); 57 58 59 /** 60 * Default constructor for test class TestVendingMachine 61 */ 62 public TestVendingMachine() 63 { 64 } 65 66 protected void setUp() 67 { 68 sodaVendor = new VendingMachine(SODA_PRICE); 69 candyBarVendor = new VendingMachine(CANDY_BAR_PRICE); 70 } 71 72 73 protected void tearDown() 74 { 75 sodaVendor = null; 76 candyBarVendor = null; 77 } 78 79 80 public void testConstructor() 81 { 82 // Existence test 83 assertNotNull("Candy bar vendor exists", 84 candyBarVendor); 85 assertNotNull("Soda vendor exists", 86 sodaVendor); 87 // Type test 88 assertTrue("Candy bar vandor is a vending machine", 89 candyBarVendor instanceof VendingMachine); 90 assertTrue("Soda vandor is a vending machine", 91 sodaVendor instanceof VendingMachine); 92 } 93 94 public void testGetItemPrice() 95 { 96 assertEquals("Soda costs " + SODA_PRICE, 97 SODA_PRICE, 98 sodaVendor.getItemPrice()); 99 assertEquals("Candy bar costs " + CANDY_BAR_PRICE, 100 CANDY_BAR_PRICE, 101 candyBarVendor.getItemPrice()); 102 } 103 104 public void testBalanceIsInitiallyZero() 105 { 106 assertEquals("Balance of soda vending machine is initially zero", 107 0, 108 sodaVendor.getCurrentBalance()); 109 } 110 111 public void testBalanceIsIncreasedByCoinValue() 112 { 113 int coin = 10; 114 sodaVendor.insertCoin(coin); 115 assertEquals("Balance of soda vending machine is equal to " + coin, 116 coin, 117 sodaVendor.getCurrentBalance()); 118 } 119 120 public void testBalanceIsCumulative() 121 { 122 int coin = 10; 123 sodaVendor.insertCoin(coin); 124 sodaVendor.insertCoin(coin); 125 assertEquals("Balance of soda vending machine is equal to " + 2 * coin, 126 2 * coin, 127 sodaVendor.getCurrentBalance()); 128 } 129 130 public void testBalanceReturnsToZeroOnVending() 131 { 132 sodaVendor.insertCoin(50); 133 sodaVendor.insertCoin(20); 134 sodaVendor.insertCoin(5); 135 // The price is right! 136 assertEquals("We have entered correct money", 137 SODA_PRICE, 138 sodaVendor.getCurrentBalance()); 139 sodaVendor.dispenseItem(); 140 assertEquals("After vending, the balance of soda vending machine is zero", 141 0, 142 sodaVendor.getCurrentBalance()); 143 } 144 145 146 public void testInsertOneOfEachCoin() { 147 int valueOfCoins = insertCoins(candyBarVendor, coinage); 148 assertEquals("Coins to value of " + valueOfCoins + " has been inserted", 149 valueOfCoins, 150 candyBarVendor.getCurrentBalance()); 151 152 } 153 154 private int[] correctMoneyForASoda = {FIVE_P, TWENTY_P, FIFTY_P}; 155 156 public void testCorrectMoneyInsertedForASoda() { 157 int costOfASoda = insertCoins(sodaVendor, correctMoneyForASoda); 158 assertEquals("Money inserted for a soda matches vending price", 159 SODA_PRICE, 160 costOfASoda); 161 assertEquals("Balance is also correct", 162 SODA_PRICE, 163 sodaVendor.getCurrentBalance()); 164 } 165 166 private int[] correctMoneyForACandyBar = {ONE_P, TWO_P, TWO_P, FIVE_P, FIVE_P, FIVE_P, TEN_P, TEN_P, TWENTY_P}; 167 public void testCorrectMoneyInsertedForACandyBar() { 168 int costOfACandyBar = insertCoins(candyBarVendor, correctMoneyForACandyBar); 169 assertEquals("Money inserted for a candy bar matches vending price", 170 CANDY_BAR_PRICE, 171 costOfACandyBar); 172 assertEquals("Balance is also correct", 173 CANDY_BAR_PRICE, 174 candyBarVendor.getCurrentBalance()); 175 } 176 177 public void testBalanceForARandmonSequenceOfCoins() 178 { 179 // Create an array of up to 100 coins 180 int[] coins = new int[generator.nextInt(100)]; 181 // Populate the array with coins 182 for (int i = 0; i < coins.length; i++) 183 { // Generate a random coin from the currency collection 184 coins[i] = coinage[generator.nextInt(coinage.length)]; 185 } 186 // now feed the candy machine and validate result 187 int totalInserted = insertCoins(candyBarVendor, coins); 188 assertEquals("Total entered is correct", 189 totalInserted, 190 candyBarVendor.getCurrentBalance()); 191 } 192 193 194 public void testVending() 195 { 196 int valueOfCoins = insertCoins(sodaVendor, new int[]{50, 20, 5}); 197 assertEquals("Coins equals item price", valueOfCoins, sodaVendor.getItemPrice()); 198 sodaVendor.dispenseItem(); 199 assertEquals("Balance should now be 0", 0, sodaVendor.getCurrentBalance()); 200 } 201 202 public void testNothingVendedIfInsufficientCoins() 203 { 204 candyBarVendor.insertCoin(CANDY_BAR_PRICE - 10); 205 candyBarVendor.dispenseItem(); 206 assertEquals(CANDY_BAR_PRICE - 10, candyBarVendor.getCurrentBalance()); 207 candyBarVendor.insertCoin(10); 208 assertEquals(CANDY_BAR_PRICE, candyBarVendor.getCurrentBalance()); 209 candyBarVendor.dispenseItem(); 210 assertEquals(0, candyBarVendor.getCurrentBalance()); 211 } 212 213 public void testVendingLeavesChange() 214 { 215 candyBarVendor.insertCoin(50); 216 candyBarVendor.insertCoin(50); 217 candyBarVendor.dispenseItem(); 218 assertEquals(100 - CANDY_BAR_PRICE, candyBarVendor.getCurrentBalance()); 219 } 220 221 public void testRefundGivesChange() 222 { 223 candyBarVendor.insertCoin(50); 224 candyBarVendor.insertCoin(50); 225 candyBarVendor.dispenseItem(); 226 assertEquals(100 - CANDY_BAR_PRICE, candyBarVendor.refundCurrentBalance()); 227 assertEquals(0, candyBarVendor.getCurrentBalance()); 228 } 229 230 public void testRefundEmptiesCurrentBalance() 231 { 232 candyBarVendor.insertCoin(50); 233 candyBarVendor.insertCoin(50); 234 candyBarVendor.dispenseItem(); 235 candyBarVendor.refundCurrentBalance(); 236 assertEquals(0, candyBarVendor.getCurrentBalance()); 237 } 238 239 public void testZeroCoinRejected() 240 { 241 sodaVendor.insertCoin(50); 242 sodaVendor.insertCoin(0); 243 assertEquals(50, sodaVendor.getCurrentBalance()); 244 } 245 246 public void testNegativeCoinRejected() 247 { 248 sodaVendor.insertCoin(50); 249 sodaVendor.insertCoin(-1); 250 assertEquals(50, sodaVendor.getCurrentBalance()); 251 } 252 253 254 255 private int insertCoins(VendingMachine vm, int[] coins) 256 { 257 int coinsInserted = 0; 258 for (int coin: coins) 259 { 260 vm.insertCoin(coin); 261 coinsInserted += coin; 262 } 263 return coinsInserted; 264 } 265 266 267 } 268
