1
+ package com .codedifferently .lesson17 .bank ;
2
+
3
+ import java .util .HashSet ;
4
+ import java .util .Set ;
5
+ import java .util .UUID ;
6
+
7
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
8
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
10
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
11
+ import org .junit .jupiter .api .BeforeEach ;
12
+ import org .junit .jupiter .api .Test ;
13
+
14
+ import com .codedifferently .lesson17 .bank .exceptions .InsufficientFundsException ;
15
+
16
+ public class SavingsAccountTest {
17
+
18
+ private SavingsAccount classUnderTest ;
19
+ private Set <Customer > owners ;
20
+
21
+ @ BeforeEach
22
+ void setUp () {
23
+ owners = new HashSet <>();
24
+ owners .add (new Customer (UUID .randomUUID (), "John Doe" ));
25
+ owners .add (new Customer (UUID .randomUUID (), "Jane Smith" ));
26
+ classUnderTest = new SavingsAccount ("123456789" , owners , 100.0 );
27
+ }
28
+
29
+ @ Test
30
+ void getAccountNumber () {
31
+ assertEquals ("123456789" , classUnderTest .getAccountNumber ());
32
+ }
33
+
34
+ @ Test
35
+ void getOwners () {
36
+ assertEquals (owners , classUnderTest .getOwners ());
37
+ }
38
+
39
+ @ Test
40
+ void deposit () {
41
+ classUnderTest .deposit (50.0 );
42
+ assertEquals (150.0 , classUnderTest .getBalance ());
43
+ }
44
+
45
+ @ Test
46
+ void deposit_withNegativeAmount () {
47
+ assertThatExceptionOfType (IllegalArgumentException .class )
48
+ .isThrownBy (() -> classUnderTest .deposit (-50.0 ));
49
+ }
50
+
51
+ @ Test
52
+ void withdraw () {
53
+ classUnderTest .withdraw (50.0 );
54
+ assertEquals (50.0 , classUnderTest .getBalance ());
55
+ }
56
+
57
+ @ Test
58
+ void withdraw_withNegativeAmount () {
59
+ assertThatExceptionOfType (IllegalStateException .class )
60
+ .isThrownBy (() -> classUnderTest .withdraw (-50.0 ))
61
+ .withMessage ("Withdrawal amount must be positive" );
62
+ }
63
+
64
+ @ Test
65
+ void withdraw_withInsufficientBalance () {
66
+ assertThatExceptionOfType (InsufficientFundsException .class )
67
+ .isThrownBy (() -> classUnderTest .withdraw (150.0 ))
68
+ .withMessage ("Account does not have enough funds for withdrawal" );
69
+ }
70
+
71
+ @ Test
72
+ void getBalance () {
73
+ assertEquals (100.0 , classUnderTest .getBalance ());
74
+ }
75
+
76
+ @ Test
77
+ void closeAccount_withPositiveBalance () {
78
+ assertThatExceptionOfType (IllegalStateException .class )
79
+ .isThrownBy (() -> classUnderTest .closeAccount ());
80
+ }
81
+
82
+ @ Test
83
+ void isClosed () {
84
+ assertFalse (classUnderTest .isClosed ());
85
+ classUnderTest .withdraw (100 );
86
+ classUnderTest .closeAccount ();
87
+ assertTrue (classUnderTest .isClosed ());
88
+ }
89
+
90
+ @ Test
91
+ void equals () {
92
+ CheckingAccount otherAccount = new SavingsAccount ("123456789" , owners , 200.0 );
93
+ assertEquals (classUnderTest , otherAccount );
94
+ }
95
+
96
+ @ Test
97
+ void hashCodeTest () {
98
+ CheckingAccount otherAccount = new SavingsAccount ("123456789" , owners , 200.0 );
99
+ assertEquals (classUnderTest .hashCode (), otherAccount .hashCode ());
100
+ }
101
+
102
+ @ Test
103
+ void toStringTest () {
104
+ String expected = "SavingsAccount{accountNumber='123456789', balance=100.0, isActive=true}" ;
105
+ assertEquals (expected , classUnderTest .toString ());
106
+ }
107
+ }
0 commit comments