Download code

Jump to: navigation, search

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 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Testing_Vending_Machine_(Java,_Junit)?oldid=19219
 14 */
 15 
 16 import java.util.Random;
 17 
 18 /**
 19  * The test class TestVendingMachine.
 20  *
 21  * @author  LiteratePrograms.org
 22  * @version May 2007.
 23  */
 24 public class TestVendingMachine extends junit.framework.TestCase
 25 {
 26     private VendingMachine sodaVendor;
 27     private VendingMachine candyBarVendor;
 28 
 29     private static final int SODA_PRICE = 75;
 30     private static final int CANDY_BAR_PRICE = 60;
 31     private static int 
 32         ONE_P = 1, 
 33         TWO_P = 2, 
 34         FIVE_P = 5, 
 35         TEN_P = 10, 
 36         TWENTY_P = 20, 
 37         FIFTY_P = 50,
 38         ONE_POUND = 100, 
 39         TWO_POUNDS = 200; 
 40     private int[] coinage = {ONE_P, TWO_P, FIVE_P, TEN_P, TWENTY_P, FIFTY_P, ONE_POUND, TWO_POUNDS};
 41     Random generator = new Random();
 42 
 43     /**
 44      * Default constructor for test class TestVendingMachine
 45      */
 46     public TestVendingMachine()
 47     {
 48     }
 49 
 50     protected void setUp()
 51     {
 52         sodaVendor = new VendingMachine(SODA_PRICE);
 53         candyBarVendor = new VendingMachine(CANDY_BAR_PRICE);
 54     }
 55 
 56     protected void tearDown()
 57     {
 58         sodaVendor = null;
 59         candyBarVendor = null;
 60     }
 61 
 62     public void testConstructor()
 63     {
 64         // Existence test
 65         assertNotNull("Candy bar vendor exists", 
 66             candyBarVendor);
 67         assertNotNull("Soda vendor exists", 
 68             sodaVendor);
 69         // Type test
 70         assertTrue("Candy bar vandor is a vending machine", 
 71             candyBarVendor instanceof VendingMachine);
 72         assertTrue("Soda vandor is a vending machine", 
 73             sodaVendor instanceof VendingMachine);
 74     }
 75     public void testGetItemPrice()
 76     {
 77         assertEquals("Soda costs " + SODA_PRICE, 
 78             SODA_PRICE, 
 79             sodaVendor.getItemPrice());
 80         assertEquals("Candy bar costs " + CANDY_BAR_PRICE, 
 81             CANDY_BAR_PRICE, 
 82             candyBarVendor.getItemPrice());
 83     }
 84     public void testBalanceIsInitiallyZero()
 85     {
 86         assertEquals("Balance of soda vending machine is initially zero", 
 87             0,
 88             sodaVendor.getCurrentBalance());
 89     }
 90     public void testBalanceIsIncreasedByCoinValue()
 91     {
 92         int coin = 10;
 93         sodaVendor.insertCoin(coin);
 94         assertEquals("Balance of soda vending machine is equal to " + coin, 
 95             coin,
 96             sodaVendor.getCurrentBalance());
 97     }
 98     public void testBalanceIsCumulative()
 99     {
100         int coin = 10;
101         sodaVendor.insertCoin(coin);
102         sodaVendor.insertCoin(coin);
103         assertEquals("Balance of soda vending machine is equal to " + 2 * coin, 
104             2 * coin,
105             sodaVendor.getCurrentBalance());
106     }
107     public void testBalanceReturnsToZeroOnVending()
108     {
109         sodaVendor.insertCoin(50);
110         sodaVendor.insertCoin(20);
111         sodaVendor.insertCoin(5);
112         // The price is right!
113         assertEquals("We have entered correct money", 
114             SODA_PRICE,
115             sodaVendor.getCurrentBalance());
116         sodaVendor.dispenseItem();
117         assertEquals("After vending, the balance of soda vending machine is zero", 
118             0,
119             sodaVendor.getCurrentBalance());
120     }
121     public void testInsertOneOfEachCoin() {
122        int valueOfCoins = insertCoins(candyBarVendor, coinage);
123        assertEquals("Coins to value of " + valueOfCoins + " has been inserted",
124            valueOfCoins,
125            candyBarVendor.getCurrentBalance());
126     }
127     private int[] correctMoneyForASoda = {FIVE_P, TWENTY_P, FIFTY_P};
128     public void testCorrectMoneyInsertedForASoda() {
129         int costOfASoda = insertCoins(sodaVendor, correctMoneyForASoda);
130         assertEquals("Money inserted for a soda matches vending price",
131             SODA_PRICE,
132             costOfASoda);
133         assertEquals("Balance is also correct",
134             SODA_PRICE,
135             sodaVendor.getCurrentBalance());
136     } 
137     private int[] correctMoneyForACandyBar = {ONE_P, TWO_P, TWO_P, FIVE_P, FIVE_P, FIVE_P, TEN_P, TEN_P, TWENTY_P};
138     public void testCorrectMoneyInsertedForACandyBar() {
139         int costOfACandyBar = insertCoins(candyBarVendor, correctMoneyForACandyBar);
140         assertEquals("Money inserted for a candy bar matches vending price",
141             CANDY_BAR_PRICE,
142             costOfACandyBar);
143         assertEquals("Balance is also correct",
144             CANDY_BAR_PRICE,
145             candyBarVendor.getCurrentBalance());
146     } 
147     public void testBalanceForARandmonSequenceOfCoins()
148     {
149         // Create an array of up to 100 coins
150         int[] coins = new int[generator.nextInt(100)];
151         // Populate the array with coins 
152         for (int i = 0; i < coins.length; i++)
153         { // Generate a random coin from the currency collection
154           coins[i] = coinage[generator.nextInt(coinage.length)];
155         }
156         // now feed the candy machine and validate result
157         int totalInserted = insertCoins(candyBarVendor, coins);
158         assertEquals("Total entered is correct",
159             totalInserted,
160             candyBarVendor.getCurrentBalance());
161     }
162     public void testVending()
163     {
164         int valueOfCoins = insertCoins(sodaVendor, new int[]{50, 20, 5});
165         assertEquals("Coins equals item price", valueOfCoins, sodaVendor.getItemPrice());
166         sodaVendor.dispenseItem();
167         assertEquals("Balance should now be 0", 0, sodaVendor.getCurrentBalance());
168     }
169 
170     public void testNothingVendedIfInsufficientCoins()
171     {
172         candyBarVendor.insertCoin(CANDY_BAR_PRICE - 10);
173         candyBarVendor.dispenseItem();
174         assertEquals(CANDY_BAR_PRICE - 10, candyBarVendor.getCurrentBalance());
175         candyBarVendor.insertCoin(10);
176         assertEquals(CANDY_BAR_PRICE, candyBarVendor.getCurrentBalance());
177         candyBarVendor.dispenseItem();
178         assertEquals(0, candyBarVendor.getCurrentBalance());
179     }
180     public void testVendingLeavesChange()
181     {
182         candyBarVendor.insertCoin(50);
183         candyBarVendor.insertCoin(50);
184         candyBarVendor.dispenseItem();
185         assertEquals(100 - CANDY_BAR_PRICE, candyBarVendor.getCurrentBalance());
186     }
187     public void testRefundGivesChange()
188     {
189         candyBarVendor.insertCoin(50);
190         candyBarVendor.insertCoin(50);
191         candyBarVendor.dispenseItem();
192         assertEquals(100 - CANDY_BAR_PRICE, candyBarVendor.refundCurrentBalance());
193         assertEquals(0, candyBarVendor.getCurrentBalance());
194     }
195     	
196     public void testRefundEmptiesCurrentBalance()
197     {
198         candyBarVendor.insertCoin(50);
199         candyBarVendor.insertCoin(50);
200         candyBarVendor.dispenseItem();
201         candyBarVendor.refundCurrentBalance();
202         assertEquals(0, candyBarVendor.getCurrentBalance());
203     }
204     public void testZeroCoinRejected()
205     {
206         sodaVendor.insertCoin(50);
207         sodaVendor.insertCoin(0);
208         assertEquals(50, sodaVendor.getCurrentBalance());
209     }
210 
211     public void testNegativeCoinRejected()
212     {
213         sodaVendor.insertCoin(50);
214         sodaVendor.insertCoin(-1);
215         assertEquals(50, sodaVendor.getCurrentBalance());
216     }
217 
218     	private int insertCoins(VendingMachine vm, int[] coins)
219     	{
220     	    int coinsInserted = 0;
221     		for (int coin: coins) 
222     		{
223     		    vm.insertCoin(coin);
224     		    coinsInserted += coin;
225     		}
226     		return coinsInserted;
227             }
228 
229 }


hijacker
hijacker
hijacker
hijacker