@@ -11,106 +11,108 @@ public class VendingMachineTest {
11
11
12
12
private static final Double NO_COIN = (Double ) 0.0 ;
13
13
14
- private VendingMachine vendingMachine ;
14
+ private VendingMachine underTest ;
15
15
16
+ private CurrencyHandlerStub currencyHandlerStub ;
16
17
@ Before
17
18
public void setUp () {
18
- vendingMachine = new VendingMachine ();
19
+ currencyHandlerStub = new CurrencyHandlerStub ();
20
+ underTest = new VendingMachine (currencyHandlerStub );
19
21
}
20
22
21
23
@ Test
22
24
public void shouldAcceptQuarter () {
23
25
24
- vendingMachine .insert (Coin .QUARTER );
26
+ underTest .insert (Coin .QUARTER );
25
27
26
- assertEquals (Coin .QUARTER .getValue (), vendingMachine .getCurrentAmount ());
28
+ assertEquals (Coin .QUARTER .getValue (), underTest .getCurrentAmount ());
27
29
}
28
30
29
31
@ Test
30
32
public void shouldResetCurrentAmountEqualToZeroWhenReturnCoinsIsPressed () {
31
33
32
- vendingMachine .setCurrentAmount (Coin .QUARTER .getValue ());
34
+ underTest .setCurrentAmount (Coin .QUARTER .getValue ());
33
35
34
- vendingMachine .returnCoins ();
36
+ underTest .returnCoins ();
35
37
36
- assertEquals (NO_COIN , vendingMachine .getCurrentAmount ());
38
+ assertEquals (NO_COIN , underTest .getCurrentAmount ());
37
39
}
38
40
39
41
@ Test
40
42
public void pennyShouldGoToCoinReturn () {
41
- vendingMachine .insert (Coin .PENNY );
43
+ underTest .insert (Coin .PENNY );
42
44
43
- assertEquals (Coin .PENNY .getValue (), vendingMachine .getCoinReturnAmount ());
45
+ assertEquals (Coin .PENNY .getValue (), underTest .getCoinReturnAmount ());
44
46
}
45
47
46
48
@ Test
47
49
public void shouldNotAddPennyToCurrentValue () {
48
- vendingMachine .insert (Coin .PENNY );
50
+ underTest .insert (Coin .PENNY );
49
51
50
- assertEquals (NO_COIN , vendingMachine .getCurrentAmount ());
52
+ assertEquals (NO_COIN , underTest .getCurrentAmount ());
51
53
}
52
54
53
55
@ Test
54
56
public void shouldAcceptNickel () {
55
- vendingMachine .insert (Coin .NICKEL );
57
+ underTest .insert (Coin .NICKEL );
56
58
57
- assertEquals (Coin .NICKEL .getValue (), vendingMachine .getCurrentAmount ());
59
+ assertEquals (Coin .NICKEL .getValue (), underTest .getCurrentAmount ());
58
60
}
59
61
60
62
@ Test
61
63
public void shouldAcceptDime () {
62
- vendingMachine .insert (Coin .DIME );
64
+ underTest .insert (Coin .DIME );
63
65
64
- assertEquals (Coin .DIME .getValue (), vendingMachine .getCurrentAmount ());
66
+ assertEquals (Coin .DIME .getValue (), underTest .getCurrentAmount ());
65
67
}
66
68
67
69
@ Test
68
70
public void pressingCoinReturnShouldReturnExactCoinsInserted () {
69
71
insertFiftyCentsInQuarters ();
70
- vendingMachine .insert (Coin .NICKEL );
72
+ underTest .insert (Coin .NICKEL );
71
73
72
74
List <Coin > expectedCoinList = new ArrayList <Coin >();
73
75
expectedCoinList .add (Coin .QUARTER );
74
76
expectedCoinList .add (Coin .QUARTER );
75
77
expectedCoinList .add (Coin .NICKEL );
76
78
77
- assertEquals (expectedCoinList , vendingMachine .returnCoins ());
79
+ assertEquals (expectedCoinList , underTest .returnCoins ());
78
80
}
79
81
80
82
@ Test
81
83
public void returnCoinSlotShouldHoldAllCoinsReturned () {
82
- vendingMachine .insertPenny ();
84
+ underTest .insertPenny ();
83
85
insertFiftyCentsInQuarters ();
84
- vendingMachine .insert (Coin .NICKEL );
86
+ underTest .insert (Coin .NICKEL );
85
87
86
88
List <Coin > expectedCoinList = new ArrayList <Coin >();
87
89
expectedCoinList .add (Coin .PENNY );
88
90
expectedCoinList .add (Coin .QUARTER );
89
91
expectedCoinList .add (Coin .QUARTER );
90
92
expectedCoinList .add (Coin .NICKEL );
91
93
92
- vendingMachine .returnCoins ();
94
+ underTest .returnCoins ();
93
95
94
- assertEquals (expectedCoinList , vendingMachine .getReturnSlotCoins ());
96
+ assertEquals (expectedCoinList , underTest .getReturnSlotCoins ());
95
97
}
96
98
97
99
@ Test
98
100
public void currentAmountShouldContinueToSumAllChangePutInMachine () {
99
- vendingMachine .insertPenny ();
101
+ underTest .insertPenny ();
100
102
insertFiftyCentsInQuarters ();
101
- vendingMachine .insert (Coin .NICKEL );
103
+ underTest .insert (Coin .NICKEL );
102
104
103
- assertEquals ((Double ) 0.55 , vendingMachine .getCurrentAmount ());
105
+ assertEquals ((Double ) 0.55 , underTest .getCurrentAmount ());
104
106
}
105
107
106
108
@ Test
107
109
public void displayShouldShowAmountOfCurrencyInsertedIntoMachine () {
108
110
insertDollarInQuarters ();
109
- vendingMachine .insert (Coin .DIME );
110
- vendingMachine .insert (Coin .DIME );
111
- vendingMachine .insert (Coin .NICKEL );
111
+ underTest .insert (Coin .DIME );
112
+ underTest .insert (Coin .DIME );
113
+ underTest .insert (Coin .NICKEL );
112
114
113
- assertEquals ("$1.25" , vendingMachine .getDisplay ());
115
+ assertEquals ("$1.25" , underTest .getDisplay ());
114
116
}
115
117
116
118
@ Test
@@ -121,9 +123,9 @@ public void itemBinShouldHoldSodaWhenSodaButtonIsPressed() {
121
123
ArrayList <String > expectedItemBinList = new ArrayList <String >();
122
124
expectedItemBinList .add ("Soda" );
123
125
124
- vendingMachine .sodaButton ();
126
+ underTest .sodaButton ();
125
127
126
- assertEquals (expectedItemBinList , vendingMachine .getItemBinList ());
128
+ assertEquals (expectedItemBinList , underTest .getItemBinList ());
127
129
}
128
130
129
131
@ Test
@@ -132,9 +134,9 @@ public void vendingMachineShouldNotVendSodaIfChangeIsInsufficent() {
132
134
133
135
ArrayList <String > expectedItemBinList = new ArrayList <String >();
134
136
135
- vendingMachine .sodaButton ();
137
+ underTest .sodaButton ();
136
138
137
- assertEquals (expectedItemBinList , vendingMachine .getItemBinList ());
139
+ assertEquals (expectedItemBinList , underTest .getItemBinList ());
138
140
}
139
141
140
142
@ Test
@@ -144,9 +146,9 @@ public void itemBinShouldHoldChipsWhenChipsButtonIsPressed() {
144
146
ArrayList <String > expectedItemBinList = new ArrayList <String >();
145
147
expectedItemBinList .add ("Chips" );
146
148
147
- vendingMachine .chipsButton ();
149
+ underTest .chipsButton ();
148
150
149
- assertEquals (expectedItemBinList , vendingMachine .getItemBinList ());
151
+ assertEquals (expectedItemBinList , underTest .getItemBinList ());
150
152
}
151
153
152
154
@ Test
@@ -155,9 +157,9 @@ public void vendingMachineShouldNotVendChipsIfChangeIsInsufficent() {
155
157
156
158
ArrayList <String > expectedItemBinList = new ArrayList <String >();
157
159
158
- vendingMachine .chipsButton ();
160
+ underTest .chipsButton ();
159
161
160
- assertEquals (expectedItemBinList , vendingMachine .getItemBinList ());
162
+ assertEquals (expectedItemBinList , underTest .getItemBinList ());
161
163
}
162
164
163
165
@ Test
@@ -167,51 +169,51 @@ public void itemBinShouldHoldCandyWhenCandyButtonIsPressed() {
167
169
ArrayList <String > expectedItemBinList = new ArrayList <String >();
168
170
expectedItemBinList .add ("Candy" );
169
171
170
- vendingMachine .candyButton ();
172
+ underTest .candyButton ();
171
173
172
- assertEquals (expectedItemBinList , vendingMachine .getItemBinList ());
174
+ assertEquals (expectedItemBinList , underTest .getItemBinList ());
173
175
}
174
176
175
177
@ Test
176
178
public void vendingMachineShouldNotVendCandyIfChangeIsInsufficent () {
177
- vendingMachine .insert (Coin .QUARTER );
178
- vendingMachine .insert (Coin .NICKEL );
179
+ underTest .insert (Coin .QUARTER );
180
+ underTest .insert (Coin .NICKEL );
179
181
180
182
ArrayList <String > expectedItemBinList = new ArrayList <String >();
181
183
182
- vendingMachine .candyButton ();
184
+ underTest .candyButton ();
183
185
184
- assertEquals (expectedItemBinList , vendingMachine .getItemBinList ());
186
+ assertEquals (expectedItemBinList , underTest .getItemBinList ());
185
187
}
186
188
187
189
@ Test
188
190
public void itemBinShouldHoldCandySodaChipsIfAllArePressed () {
189
191
insertDollarInQuarters ();
190
192
insertDollarInQuarters ();
191
193
insertDollarInQuarters ();
192
- vendingMachine .insert (Coin .QUARTER );
194
+ underTest .insert (Coin .QUARTER );
193
195
194
196
ArrayList <String > expectedItemBinList = new ArrayList <String >();
195
197
expectedItemBinList .add ("Candy" );
196
198
expectedItemBinList .add ("Soda" );
197
199
expectedItemBinList .add ("Chips" );
198
200
199
- vendingMachine .candyButton ();
200
- vendingMachine .sodaButton ();
201
- vendingMachine .chipsButton ();
201
+ underTest .candyButton ();
202
+ underTest .sodaButton ();
203
+ underTest .chipsButton ();
202
204
203
- assertEquals (expectedItemBinList , vendingMachine .getItemBinList ());
205
+ assertEquals (expectedItemBinList , underTest .getItemBinList ());
204
206
}
205
207
206
208
@ Test
207
209
public void machineShouldMakeChangeIfTooMuchMoneyPaidForSoda () {
208
210
insertDollarInQuarters ();
209
211
insertFiftyCentsInQuarters ();
210
212
211
- vendingMachine .sodaButton ();
212
- vendingMachine .returnCoins ();
213
+ underTest .sodaButton ();
214
+ underTest .returnCoins ();
213
215
214
- assertEquals (Coin .QUARTER .getValue (), vendingMachine .getCoinReturnAmount ());
216
+ assertEquals (Coin .QUARTER .getValue (), underTest .getCoinReturnAmount ());
215
217
}
216
218
217
219
private void insertDollarInQuarters () {
@@ -220,7 +222,35 @@ private void insertDollarInQuarters() {
220
222
}
221
223
222
224
private void insertFiftyCentsInQuarters () {
223
- vendingMachine .insert (Coin .QUARTER );
224
- vendingMachine .insert (Coin .QUARTER );
225
+ underTest .insert (Coin .QUARTER );
226
+ underTest .insert (Coin .QUARTER );
227
+ }
228
+
229
+ @ Test
230
+ public void shouldCallCurrencyHandlerWhenMoneyIsInserted (){
231
+ underTest .insert (Coin .QUARTER );
232
+ assertEquals (Coin .QUARTER , currencyHandlerStub .getCoin ());
233
+ }
234
+
235
+ public class CurrencyHandlerStub implements CurrencyHandler {
236
+ Coin coin ;
237
+ Double currentAmount = 0.0 ;
238
+ public Coin getCoin () {
239
+ return coin ;
240
+ }
241
+ @ Override
242
+ public void insert (Coin coin ){
243
+ this .coin =coin ;
244
+ this .currentAmount = getCurrentAmount () + coin .getValue ();
245
+ }
246
+ @ Override
247
+ public Double getCurrentAmount () {
248
+ return this .currentAmount ;
249
+ }
250
+ @ Override
251
+ public void setCurrentAmount (Double currentAmount ) {
252
+ this .currentAmount = currentAmount ;
253
+ }
254
+
225
255
}
226
256
}
0 commit comments