forked from Amitabh-DevOps/Springboot-BankApp
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBankControllerTest.java
More file actions
218 lines (175 loc) · 7.95 KB
/
Copy pathBankControllerTest.java
File metadata and controls
218 lines (175 loc) · 7.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
package com.example.bankapp.controller;
import com.example.bankapp.model.Account;
import com.example.bankapp.model.Transaction;
import com.example.bankapp.service.AccountService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class BankControllerTest {
private static final String USERNAME = "alice";
@Mock
private AccountService accountService;
@InjectMocks
private BankController controller;
private Account account;
@BeforeEach
void setUp() {
account = new Account();
account.setId(1L);
account.setUsername(USERNAME);
account.setBalance(new BigDecimal("100"));
}
private void authenticateAs(String username) {
SecurityContext context = new SecurityContextImpl();
context.setAuthentication(new TestingAuthenticationToken(username, "password"));
SecurityContextHolder.setContext(context);
}
@AfterEach
void tearDown() {
SecurityContextHolder.clearContext();
}
@Test
void dashboardAddsAccountToModelAndReturnsDashboardView() {
authenticateAs(USERNAME);
when(accountService.findAccountByUsername(USERNAME)).thenReturn(account);
Model model = new ExtendedModelMap();
String view = controller.dashboard(model);
assertEquals("dashboard", view);
assertSame(account, model.getAttribute("account"));
verify(accountService).findAccountByUsername(USERNAME);
}
@Test
void showRegistrationFormReturnsRegisterView() {
assertEquals("register", controller.showRegistrationForm());
verifyNoInteractions(accountService);
}
@Test
void registerAccountOnSuccessRedirectsToLogin() {
Model model = new ExtendedModelMap();
String view = controller.registerAccount(USERNAME, "secret", model);
assertEquals("redirect:/login", view);
verify(accountService).registerAccount(USERNAME, "secret");
assertEquals(null, model.getAttribute("error"));
}
@Test
void registerAccountOnRuntimeExceptionAddsErrorAndReturnsRegisterView() {
Model model = new ExtendedModelMap();
when(accountService.registerAccount(USERNAME, "secret"))
.thenThrow(new RuntimeException("Username already exists"));
String view = controller.registerAccount(USERNAME, "secret", model);
assertEquals("register", view);
assertEquals("Username already exists", model.getAttribute("error"));
verify(accountService).registerAccount(USERNAME, "secret");
}
@Test
void loginReturnsLoginView() {
assertEquals("login", controller.login());
verifyNoInteractions(accountService);
}
@Test
void depositCallsServiceAndRedirectsToDashboard() {
authenticateAs(USERNAME);
BigDecimal amount = new BigDecimal("50");
when(accountService.findAccountByUsername(USERNAME)).thenReturn(account);
String view = controller.deposit(amount);
assertEquals("redirect:/dashboard", view);
verify(accountService).findAccountByUsername(USERNAME);
verify(accountService).deposit(account, amount);
}
@Test
void withdrawOnSuccessRedirectsToDashboard() {
authenticateAs(USERNAME);
BigDecimal amount = new BigDecimal("50");
when(accountService.findAccountByUsername(USERNAME)).thenReturn(account);
Model model = new ExtendedModelMap();
String view = controller.withdraw(amount, model);
assertEquals("redirect:/dashboard", view);
verify(accountService).withdraw(account, amount);
assertEquals(null, model.getAttribute("error"));
assertEquals(null, model.getAttribute("account"));
}
@Test
void withdrawOnRuntimeExceptionAddsErrorAndAccountAndReturnsDashboardView() {
authenticateAs(USERNAME);
BigDecimal amount = new BigDecimal("500");
when(accountService.findAccountByUsername(USERNAME)).thenReturn(account);
org.mockito.Mockito.doThrow(new RuntimeException("Insufficient funds"))
.when(accountService).withdraw(account, amount);
Model model = new ExtendedModelMap();
String view = controller.withdraw(amount, model);
assertEquals("dashboard", view);
assertEquals("Insufficient funds", model.getAttribute("error"));
assertSame(account, model.getAttribute("account"));
verify(accountService).withdraw(account, amount);
}
@Test
void transactionHistoryAddsTransactionsToModelAndReturnsTransactionsView() {
authenticateAs(USERNAME);
when(accountService.findAccountByUsername(USERNAME)).thenReturn(account);
List<Transaction> transactions = Arrays.asList(
new Transaction(new BigDecimal("50"), "Deposit", LocalDateTime.now(), account),
new Transaction(new BigDecimal("20"), "Withdrawal", LocalDateTime.now(), account)
);
when(accountService.getTransactionHistory(account)).thenReturn(transactions);
Model model = new ExtendedModelMap();
String view = controller.transactionHistory(model);
assertEquals("transactions", view);
assertSame(transactions, model.getAttribute("transactions"));
verify(accountService).getTransactionHistory(account);
}
@Test
void transferAmountOnSuccessRedirectsToDashboard() {
authenticateAs(USERNAME);
BigDecimal amount = new BigDecimal("25");
when(accountService.findAccountByUsername(USERNAME)).thenReturn(account);
Model model = new ExtendedModelMap();
String view = controller.transferAmount("bob", amount, model);
assertEquals("redirect:/dashboard", view);
verify(accountService).transferAmount(account, "bob", amount);
assertEquals(null, model.getAttribute("error"));
assertEquals(null, model.getAttribute("account"));
}
@Test
void transferAmountOnRuntimeExceptionAddsErrorAndAccountAndReturnsDashboardView() {
authenticateAs(USERNAME);
BigDecimal amount = new BigDecimal("1000");
when(accountService.findAccountByUsername(USERNAME)).thenReturn(account);
org.mockito.Mockito.doThrow(new RuntimeException("Insufficient funds"))
.when(accountService).transferAmount(account, "bob", amount);
Model model = new ExtendedModelMap();
String view = controller.transferAmount("bob", amount, model);
assertEquals("dashboard", view);
assertEquals("Insufficient funds", model.getAttribute("error"));
assertSame(account, model.getAttribute("account"));
verify(accountService).transferAmount(account, "bob", amount);
verify(accountService, times(1)).findAccountByUsername(USERNAME);
}
@Test
void registerAccountDoesNotResolveSecurityContext() {
Model model = new ExtendedModelMap();
controller.registerAccount(USERNAME, "secret", model);
verify(accountService, never()).findAccountByUsername(USERNAME);
}
}