-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathBankControllerMvcTest.java
163 lines (125 loc) · 5.13 KB
/
BankControllerMvcTest.java
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
package com.jayway.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.service.AccountService;
import com.jayway.service.ImmutableAccount;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(MockitoJUnitRunner.class)
public class BankControllerMvcTest {
@Mock
AccountService accountServiceMock;
MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders
.standaloneSetup(new BankController(accountServiceMock))
.build();
}
@Test
public void shouldPrint() throws Exception {
ImmutableAccount account = new ImmutableAccount(1, 100);
when(accountServiceMock.get(1)).thenReturn(account);
mockMvc
.perform(get("/accounts/1")
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print());
}
@Test
public void shouldGetAccount() throws Exception {
ImmutableAccount account = new ImmutableAccount(1, 100);
when(accountServiceMock.get(1)).thenReturn(account);
mockMvc
.perform(get("/accounts/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("accountNumber").value(1))
.andExpect(jsonPath("balance").value(100));
}
@Test
public void shouldGetAllAccounts() throws Exception {
when(accountServiceMock.getAllAccountNumbers()).thenReturn(Arrays.asList(1, 2));
mockMvc
.perform(get("/accounts")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("[0]").value(1))
.andExpect(jsonPath("[1]").value(2));
}
@Test
public void shouldDepositToAccount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", 50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/deposit")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isNoContent());
verify(accountServiceMock).deposit(1, 50);
}
@Test
public void shouldDeleteAccount() throws Exception {
mockMvc
.perform(delete("/accounts/1"))
.andExpect(status().isNoContent());
verify(accountServiceMock).deleteAccount(1);
}
@Test
public void shouldNotDepositNegativeAmount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", -50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/deposit")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest());
verifyZeroInteractions(accountServiceMock);
}
@Test
public void shouldWithdrawFromAccount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", 50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/withdraw")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk());
verify(accountServiceMock).withdraw(1, 50);
}
@Test
public void shouldNotWithdrawNegativeAmount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", -50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/withdraw")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest());
verifyZeroInteractions(accountServiceMock);
}
private String toJsonString(Map<String, ?> map) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}