1
1
package com .codedifferently .lesson17 .bank ;
2
-
3
2
import static org .assertj .core .api .Assertions .assertThat ;
4
3
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
5
4
6
- import com .codedifferently .lesson17 .bank .exceptions .AccountNotFoundException ;
7
- import com .codedifferently .lesson17 .bank .exceptions .CheckVoidedException ;
8
5
import java .util .Set ;
9
6
import java .util .UUID ;
7
+
10
8
import org .junit .jupiter .api .BeforeEach ;
11
9
import org .junit .jupiter .api .Test ;
12
10
11
+ import com .codedifferently .lesson17 .bank .exceptions .AccountNotFoundException ;
12
+ import com .codedifferently .lesson17 .bank .exceptions .CheckVoidedException ;
13
13
class BankAtmTest {
14
14
15
15
private BankAtm classUnderTest ;
16
16
private CheckingAccount account1 ;
17
- private CheckingAccount account2 ;
17
+ private SavingAccount account2 ;
18
18
private Customer customer1 ;
19
19
private Customer customer2 ;
20
20
21
- @ BeforeEach
21
+ @ BeforeEach
22
22
void setUp () {
23
23
classUnderTest = new BankAtm ();
24
24
customer1 = new Customer (UUID .randomUUID (), "John Doe" );
25
25
customer2 = new Customer (UUID .randomUUID (), "Jane Smith" );
26
26
account1 = new CheckingAccount ("123456789" , Set .of (customer1 ), 100.0 );
27
- account2 = new CheckingAccount ("987654321" , Set .of (customer1 , customer2 ), 200.0 );
27
+ account2 = new SavingAccount ("987654321" , Set .of (customer1 , customer2 ), 200.0 );
28
28
customer1 .addAccount (account1 );
29
29
customer1 .addAccount (account2 );
30
30
customer2 .addAccount (account2 );
31
31
classUnderTest .addAccount (account1 );
32
32
classUnderTest .addAccount (account2 );
33
33
}
34
-
35
34
@ Test
36
35
void testAddAccount () {
37
36
// Arrange
38
37
Customer customer3 = new Customer (UUID .randomUUID (), "Alice Johnson" );
39
38
CheckingAccount account3 = new CheckingAccount ("555555555" , Set .of (customer3 ), 300.0 );
40
39
customer3 .addAccount (account3 );
41
-
42
40
// Act
43
41
classUnderTest .addAccount (account3 );
44
42
45
43
// Assert
46
- Set <CheckingAccount > accounts = classUnderTest .findAccountsByCustomerId (customer3 .getId ());
44
+ Set <BankAccount > accounts = classUnderTest .findAccountsByCustomerId (customer3 .getId ());
47
45
assertThat (accounts ).containsOnly (account3 );
48
46
}
49
47
50
48
@ Test
51
49
void testFindAccountsByCustomerId () {
52
50
// Act
53
- Set <CheckingAccount > accounts = classUnderTest .findAccountsByCustomerId (customer1 .getId ());
51
+ Set <BankAccount > accounts = classUnderTest .findAccountsByCustomerId (customer1 .getId ());
54
52
55
53
// Assert
56
54
assertThat (accounts ).containsOnly (account1 , account2 );
57
55
}
58
-
59
56
@ Test
60
57
void testDepositFunds () {
61
58
// Act
62
59
classUnderTest .depositFunds (account1 .getAccountNumber (), 50.0 );
63
-
64
60
// Assert
65
61
assertThat (account1 .getBalance ()).isEqualTo (150.0 );
66
62
}
67
-
68
63
@ Test
69
64
void testDepositFunds_Check () {
70
65
// Arrange
71
66
Check check = new Check ("987654321" , 100.0 , account1 );
72
-
73
67
// Act
74
68
classUnderTest .depositFunds ("987654321" , check );
75
-
76
69
// Assert
77
70
assertThat (account1 .getBalance ()).isEqualTo (0 );
78
71
assertThat (account2 .getBalance ()).isEqualTo (300.0 );
79
72
}
80
-
81
73
@ Test
82
74
void testDepositFunds_DoesntDepositCheckTwice () {
83
75
Check check = new Check ("987654321" , 100.0 , account1 );
84
-
85
76
classUnderTest .depositFunds ("987654321" , check );
86
-
87
77
assertThatExceptionOfType (CheckVoidedException .class )
88
78
.isThrownBy (() -> classUnderTest .depositFunds ("987654321" , check ))
89
79
.withMessage ("Check is voided" );
90
80
}
91
-
92
81
@ Test
93
82
void testWithdrawFunds () {
94
83
// Act
95
84
classUnderTest .withdrawFunds (account2 .getAccountNumber (), 50.0 );
96
-
97
85
// Assert
98
86
assertThat (account2 .getBalance ()).isEqualTo (150.0 );
99
87
}
100
-
101
88
@ Test
102
89
void testWithdrawFunds_AccountNotFound () {
103
90
String nonExistingAccountNumber = "999999999" ;
104
-
105
91
// Act & Assert
106
92
assertThatExceptionOfType (AccountNotFoundException .class )
107
93
.isThrownBy (() -> classUnderTest .withdrawFunds (nonExistingAccountNumber , 50.0 ))
108
94
.withMessage ("Account not found" );
109
95
}
110
- }
96
+ }
0 commit comments