1
- import static org .assertj .core .api .Assertions .assertThat ;
2
- import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
3
- import static org .assertj .core .api .Assertions .fail ;
4
-
1
+ import org .junit .Before ;
5
2
import org .junit .Ignore ;
6
3
import org .junit .Test ;
7
4
8
5
import java .util .Random ;
9
6
7
+ import static org .assertj .core .api .Assertions .*;
8
+
10
9
public class BankAccountTest {
11
- private BankAccount bankAccount = new BankAccount ();
10
+ private BankAccount bankAccount ;
11
+
12
+ @ Before
13
+ public void setUp () {
14
+ bankAccount = new BankAccount ();
15
+ }
12
16
13
17
@ Test
14
18
public void newlyOpenedAccountHasEmptyBalance () throws BankAccountActionInvalidException {
@@ -19,136 +23,162 @@ public void newlyOpenedAccountHasEmptyBalance() throws BankAccountActionInvalidE
19
23
20
24
@ Ignore ("Remove to run test" )
21
25
@ Test
22
- public void canDepositMoney () throws BankAccountActionInvalidException {
26
+ public void singleDeposit () throws BankAccountActionInvalidException {
23
27
bankAccount .open ();
28
+ bankAccount .deposit (100 );
24
29
25
- bankAccount .deposit (10 );
26
-
27
- assertThat (bankAccount .getBalance ()).isEqualTo (10 );
30
+ assertThat (bankAccount .getBalance ()).isEqualTo (100 );
28
31
}
29
32
30
33
@ Ignore ("Remove to run test" )
31
34
@ Test
32
- public void canDepositMoneySequentially () throws BankAccountActionInvalidException {
35
+ public void multipleDeposits () throws BankAccountActionInvalidException {
33
36
bankAccount .open ();
37
+ bankAccount .deposit (100 );
38
+ bankAccount .deposit (50 );
34
39
35
- bankAccount .deposit (5 );
36
- bankAccount .deposit (23 );
37
-
38
- assertThat (bankAccount .getBalance ()).isEqualTo (28 );
40
+ assertThat (bankAccount .getBalance ()).isEqualTo (150 );
39
41
}
40
42
41
43
@ Ignore ("Remove to run test" )
42
44
@ Test
43
- public void canWithdrawMoney () throws BankAccountActionInvalidException {
45
+ public void withdrawOnce () throws BankAccountActionInvalidException {
44
46
bankAccount .open ();
45
- bankAccount .deposit (10 );
46
-
47
- bankAccount .withdraw (5 );
47
+ bankAccount .deposit (100 );
48
+ bankAccount .withdraw (75 );
48
49
49
- assertThat (bankAccount .getBalance ()).isEqualTo (5 );
50
+ assertThat (bankAccount .getBalance ()).isEqualTo (25 );
50
51
}
51
52
52
53
@ Ignore ("Remove to run test" )
53
54
@ Test
54
- public void canWithdrawMoneySequentially () throws BankAccountActionInvalidException {
55
+ public void withdrawTwice () throws BankAccountActionInvalidException {
55
56
bankAccount .open ();
56
- bankAccount .deposit (23 );
57
-
58
- bankAccount .withdraw (10 );
59
- bankAccount .withdraw (13 );
57
+ bankAccount .deposit (100 );
58
+ bankAccount .withdraw (80 );
59
+ bankAccount .withdraw (20 );
60
60
61
61
assertThat (bankAccount .getBalance ()).isEqualTo (0 );
62
62
}
63
63
64
64
@ Ignore ("Remove to run test" )
65
65
@ Test
66
- public void cannotWithdrawMoneyFromEmptyAccount () {
66
+ public void canDoMultipleOperationsSequentially () throws BankAccountActionInvalidException {
67
67
bankAccount .open ();
68
+ bankAccount .deposit (100 );
69
+ bankAccount .deposit (110 );
70
+ bankAccount .withdraw (200 );
71
+ bankAccount .deposit (60 );
72
+ bankAccount .withdraw (50 );
68
73
69
- assertThatExceptionOfType (BankAccountActionInvalidException .class )
70
- .isThrownBy (() -> bankAccount .withdraw (5 ))
71
- .withMessage ("Cannot withdraw money from an empty account" );
74
+ assertThat (bankAccount .getBalance ()).isEqualTo (20 );
72
75
}
73
76
74
77
@ Ignore ("Remove to run test" )
75
78
@ Test
76
- public void cannotWithdrawMoreMoneyThanYouHave () throws BankAccountActionInvalidException {
79
+ public void cannotCheckBalanceOfClosedAccount () throws BankAccountActionInvalidException {
77
80
bankAccount .open ();
78
- bankAccount .deposit ( 6 );
81
+ bankAccount .close ( );
79
82
80
83
assertThatExceptionOfType (BankAccountActionInvalidException .class )
81
- . isThrownBy (() -> bankAccount . withdraw ( 7 ) )
82
- .withMessage ("Cannot withdraw more money than is currently in the account " );
84
+ . isThrownBy ( bankAccount :: getBalance )
85
+ .withMessage ("Account closed " );
83
86
}
84
87
85
88
@ Ignore ("Remove to run test" )
86
89
@ Test
87
- public void cannotDepositNegativeAmount () {
90
+ public void cannotDepositIntoClosedAccount () throws BankAccountActionInvalidException {
88
91
bankAccount .open ();
92
+ bankAccount .close ();
89
93
90
94
assertThatExceptionOfType (BankAccountActionInvalidException .class )
91
- .isThrownBy (() -> bankAccount .deposit (- 1 ))
92
- .withMessage ("Cannot deposit or withdraw negative amount " );
95
+ .isThrownBy (() -> bankAccount .deposit (50 ))
96
+ .withMessage ("Account closed " );
93
97
}
94
98
95
99
@ Ignore ("Remove to run test" )
96
100
@ Test
97
- public void cannotWithdrawNegativeAmount () throws BankAccountActionInvalidException {
101
+ public void cannotDepositIntoUnopenedAccount () {
102
+ assertThatExceptionOfType (BankAccountActionInvalidException .class )
103
+ .isThrownBy (() -> bankAccount .deposit (50 ))
104
+ .withMessage ("Account closed" );
105
+ }
106
+
107
+ @ Ignore ("Remove to run test" )
108
+ @ Test
109
+ public void cannotWithdrawFromClosedAccount () throws BankAccountActionInvalidException {
98
110
bankAccount .open ();
99
- bankAccount .deposit ( 105 );
111
+ bankAccount .close ( );
100
112
101
113
assertThatExceptionOfType (BankAccountActionInvalidException .class )
102
- .isThrownBy (() -> bankAccount .withdraw (- 5 ))
103
- .withMessage ("Cannot deposit or withdraw negative amount " );
114
+ .isThrownBy (() -> bankAccount .withdraw (50 ))
115
+ .withMessage ("Account closed " );
104
116
}
105
117
106
118
@ Ignore ("Remove to run test" )
107
119
@ Test
108
- public void cannotGetBalanceOfClosedAccount () throws BankAccountActionInvalidException {
120
+ public void cannotCloseAnAccountThatWasNotOpened () {
121
+ assertThatExceptionOfType (BankAccountActionInvalidException .class )
122
+ .isThrownBy (bankAccount ::close )
123
+ .withMessage ("Account not open" );
124
+ }
125
+
126
+ @ Ignore ("Remove to run test" )
127
+ @ Test
128
+ public void cannotOpenAnAlreadyOpenedAccount () throws BankAccountActionInvalidException {
109
129
bankAccount .open ();
110
- bankAccount .deposit (10 );
111
- bankAccount .close ();
112
130
113
131
assertThatExceptionOfType (BankAccountActionInvalidException .class )
114
- .isThrownBy (bankAccount ::getBalance )
115
- .withMessage ("Account closed " );
132
+ .isThrownBy (bankAccount ::open )
133
+ .withMessage ("Account already open " );
116
134
}
117
135
118
136
@ Ignore ("Remove to run test" )
119
137
@ Test
120
- public void cannotDepositMoneyIntoClosedAccount () {
138
+ public void reopenedAccountDoesNotRetainBalance () throws BankAccountActionInvalidException {
121
139
bankAccount .open ();
140
+ bankAccount .deposit (50 );
122
141
bankAccount .close ();
142
+ bankAccount .open ();
143
+
144
+ assertThat (bankAccount .getBalance ()).isEqualTo (0 );
145
+ }
146
+
147
+ @ Ignore ("Remove to run test" )
148
+ @ Test
149
+ public void cannotWithdrawMoreThanWasDeposited () throws BankAccountActionInvalidException {
150
+ bankAccount .open ();
151
+ bankAccount .deposit (25 );
123
152
124
153
assertThatExceptionOfType (BankAccountActionInvalidException .class )
125
- .isThrownBy (() -> bankAccount .deposit ( 5 ))
126
- .withMessage ("Account closed " );
154
+ .isThrownBy (() -> bankAccount .withdraw ( 50 ))
155
+ .withMessage ("Cannot withdraw more money than is currently in the account " );
127
156
}
128
157
129
158
@ Ignore ("Remove to run test" )
130
159
@ Test
131
- public void cannotWithdrawMoneyFromClosedAccount () throws BankAccountActionInvalidException {
160
+ public void cannotWithdrawNegativeAmount () throws BankAccountActionInvalidException {
132
161
bankAccount .open ();
133
- bankAccount .deposit (20 );
134
- bankAccount .close ();
162
+ bankAccount .deposit (100 );
135
163
136
164
assertThatExceptionOfType (BankAccountActionInvalidException .class )
137
- .isThrownBy (() -> bankAccount .withdraw (5 ))
138
- .withMessage ("Account closed " );
165
+ .isThrownBy (() -> bankAccount .withdraw (- 50 ))
166
+ .withMessage ("Cannot deposit or withdraw negative amount " );
139
167
}
140
168
141
169
@ Ignore ("Remove to run test" )
142
170
@ Test
143
- public void bankAccountIsClosedBeforeItIsOpened () {
171
+ public void cannotDepositNegativeAmount () throws BankAccountActionInvalidException {
172
+ bankAccount .open ();
173
+
144
174
assertThatExceptionOfType (BankAccountActionInvalidException .class )
145
- .isThrownBy (bankAccount :: getBalance )
146
- .withMessage ("Account closed " );
175
+ .isThrownBy (() -> bankAccount . deposit (- 50 ) )
176
+ .withMessage ("Cannot deposit or withdraw negative amount " );
147
177
}
148
178
149
179
@ Ignore ("Remove to run test" )
150
180
@ Test
151
- public void canAdjustBalanceConcurrently () throws BankAccountActionInvalidException , InterruptedException {
181
+ public void canHandleConcurrentTransactions () throws BankAccountActionInvalidException , InterruptedException {
152
182
bankAccount .open ();
153
183
bankAccount .deposit (1000 );
154
184
@@ -158,7 +188,7 @@ public void canAdjustBalanceConcurrently() throws BankAccountActionInvalidExcept
158
188
}
159
189
}
160
190
161
- private void adjustBalanceConcurrently () throws BankAccountActionInvalidException , InterruptedException {
191
+ private void adjustBalanceConcurrently () throws InterruptedException {
162
192
Random random = new Random ();
163
193
164
194
Thread [] threads = new Thread [1000 ];
0 commit comments