Skip to content

Commit 1494a09

Browse files
author
“A1-4U2T1NN”
committed
test: added general test for 'SavingsAccount' class;
1 parent e2b48dd commit 1494a09

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
import java.util.UUID;
11+
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
16+
17+
public class SavingsAccountTest {
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+
SavingsAccount otherAccount = new SavingsAccount("123456789", owners, 200.0);
93+
assertEquals(classUnderTest, otherAccount);
94+
}
95+
96+
@Test
97+
void hashCodeTest() {
98+
SavingsAccount 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

Comments
 (0)