forked from Amitabh-DevOps/Springboot-BankApp
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAccountServiceTest.java
More file actions
259 lines (190 loc) · 8.95 KB
/
Copy pathAccountServiceTest.java
File metadata and controls
259 lines (190 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.example.bankapp.service;
import com.example.bankapp.model.Account;
import com.example.bankapp.model.Transaction;
import com.example.bankapp.repository.AccountRepository;
import com.example.bankapp.repository.TransactionRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class AccountServiceTest {
@Mock
private AccountRepository accountRepository;
@Mock
private TransactionRepository transactionRepository;
@Mock
private PasswordEncoder passwordEncoder;
@InjectMocks
private AccountService accountService;
private Account account;
@BeforeEach
void setUp() {
account = new Account();
account.setId(1L);
account.setUsername("alice");
account.setPassword("secret");
account.setBalance(new BigDecimal("100"));
}
// ----- deposit() -----
@Test
void deposit_shouldIncreaseBalanceByAmount() {
accountService.deposit(account, new BigDecimal("50"));
assertEquals(new BigDecimal("150"), account.getBalance());
verify(accountRepository).save(account);
verify(transactionRepository).save(any(Transaction.class));
}
@Test
void deposit_shouldCreateTransactionWithCorrectType() {
accountService.deposit(account, new BigDecimal("50"));
ArgumentCaptor<Transaction> captor = ArgumentCaptor.forClass(Transaction.class);
verify(transactionRepository).save(captor.capture());
Transaction saved = captor.getValue();
assertEquals("Deposit", saved.getType());
assertEquals(new BigDecimal("50"), saved.getAmount());
}
// ----- withdraw() -----
@Test
void withdraw_shouldDecreaseBalanceByAmount() {
accountService.withdraw(account, new BigDecimal("30"));
assertEquals(new BigDecimal("70"), account.getBalance());
verify(accountRepository).save(account);
verify(transactionRepository).save(any(Transaction.class));
}
@Test
void withdraw_shouldThrowRuntimeExceptionWhenInsufficientFunds() {
account.setBalance(new BigDecimal("10"));
RuntimeException ex = assertThrows(RuntimeException.class,
() -> accountService.withdraw(account, new BigDecimal("50")));
assertEquals("Insufficient funds", ex.getMessage());
verify(accountRepository, never()).save(any(Account.class));
verify(transactionRepository, never()).save(any(Transaction.class));
}
@Test
void withdraw_shouldCreateTransactionWithCorrectType() {
accountService.withdraw(account, new BigDecimal("30"));
ArgumentCaptor<Transaction> captor = ArgumentCaptor.forClass(Transaction.class);
verify(transactionRepository).save(captor.capture());
Transaction saved = captor.getValue();
assertEquals("Withdrawal", saved.getType());
assertEquals(new BigDecimal("30"), saved.getAmount());
}
// ----- transferAmount() -----
@Test
void transferAmount_shouldDeductFromSenderAndAddToRecipient() {
account.setBalance(new BigDecimal("200"));
Account recipient = new Account();
recipient.setId(2L);
recipient.setUsername("bob");
recipient.setBalance(new BigDecimal("0"));
when(accountRepository.findByUsername("bob")).thenReturn(Optional.of(recipient));
accountService.transferAmount(account, "bob", new BigDecimal("50"));
assertEquals(new BigDecimal("150"), account.getBalance());
assertEquals(new BigDecimal("50"), recipient.getBalance());
}
@Test
void transferAmount_shouldThrowWhenInsufficientFunds() {
account.setBalance(new BigDecimal("10"));
RuntimeException ex = assertThrows(RuntimeException.class,
() -> accountService.transferAmount(account, "bob", new BigDecimal("50")));
assertEquals("Insufficient funds", ex.getMessage());
verify(transactionRepository, never()).save(any(Transaction.class));
}
@Test
void transferAmount_shouldThrowWhenRecipientNotFound() {
account.setBalance(new BigDecimal("200"));
when(accountRepository.findByUsername("bob")).thenReturn(Optional.empty());
RuntimeException ex = assertThrows(RuntimeException.class,
() -> accountService.transferAmount(account, "bob", new BigDecimal("50")));
assertEquals("Recipient account not found", ex.getMessage());
}
@Test
void transferAmount_shouldCreateTwoTransactionRecords() {
account.setBalance(new BigDecimal("200"));
Account recipient = new Account();
recipient.setId(2L);
recipient.setUsername("bob");
recipient.setBalance(new BigDecimal("0"));
when(accountRepository.findByUsername("bob")).thenReturn(Optional.of(recipient));
accountService.transferAmount(account, "bob", new BigDecimal("50"));
verify(transactionRepository, times(2)).save(any(Transaction.class));
}
// ----- registerAccount() -----
@Test
void registerAccount_shouldCreateAccountWithZeroBalance() {
when(accountRepository.findByUsername("newuser")).thenReturn(Optional.empty());
when(passwordEncoder.encode("password")).thenReturn("encoded");
when(accountRepository.save(any(Account.class))).thenAnswer(invocation -> invocation.getArgument(0));
accountService.registerAccount("newuser", "password");
ArgumentCaptor<Account> captor = ArgumentCaptor.forClass(Account.class);
verify(accountRepository).save(captor.capture());
Account saved = captor.getValue();
assertEquals("newuser", saved.getUsername());
assertEquals("encoded", saved.getPassword());
assertEquals(BigDecimal.ZERO, saved.getBalance());
}
@Test
void registerAccount_shouldThrowWhenUsernameAlreadyExists() {
when(accountRepository.findByUsername("alice")).thenReturn(Optional.of(account));
RuntimeException ex = assertThrows(RuntimeException.class,
() -> accountService.registerAccount("alice", "password"));
assertEquals("Username already exists", ex.getMessage());
verify(accountRepository, never()).save(any(Account.class));
}
// ----- findAccountByUsername() -----
@Test
void findAccountByUsername_shouldReturnAccountWhenFound() {
when(accountRepository.findByUsername("alice")).thenReturn(Optional.of(account));
Account result = accountService.findAccountByUsername("alice");
assertSame(account, result);
}
@Test
void findAccountByUsername_shouldThrowWhenNotFound() {
when(accountRepository.findByUsername("ghost")).thenReturn(Optional.empty());
RuntimeException ex = assertThrows(RuntimeException.class,
() -> accountService.findAccountByUsername("ghost"));
assertEquals("Account not found", ex.getMessage());
}
// ----- loadUserByUsername() -----
@Test
void loadUserByUsername_shouldReturnUserDetailsWhenFound() {
when(accountRepository.findByUsername("alice")).thenReturn(Optional.of(account));
UserDetails userDetails = accountService.loadUserByUsername("alice");
assertEquals("alice", userDetails.getUsername());
assertEquals("secret", userDetails.getPassword());
}
@Test
void loadUserByUsername_shouldThrowWhenUsernameNotFound() {
when(accountRepository.findByUsername("ghost")).thenReturn(Optional.empty());
assertThrows(RuntimeException.class,
() -> accountService.loadUserByUsername("ghost"));
}
// ----- getTransactionHistory() -----
@Test
void getTransactionHistory_shouldReturnTransactionsForAccount() {
Transaction t1 = new Transaction(new BigDecimal("50"), "Deposit", LocalDateTime.now(), account);
Transaction t2 = new Transaction(new BigDecimal("30"), "Withdrawal", LocalDateTime.now(), account);
List<Transaction> transactions = List.of(t1, t2);
when(transactionRepository.findByAccountId(1L)).thenReturn(transactions);
List<Transaction> result = accountService.getTransactionHistory(account);
assertEquals(2, result.size());
assertEquals(transactions, result);
}
}