Skip to content

Commit 5edc2f9

Browse files
committed
Readd spring context to test controller
1 parent a65eca2 commit 5edc2f9

File tree

5 files changed

+71
-193
lines changed

5 files changed

+71
-193
lines changed

src/main/java/br/com/clickbus/challenge/controller/PlaceController.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@
66
import br.com.clickbus.challenge.exception.PlaceNotFoundException;
77
import br.com.clickbus.challenge.service.PlaceService;
88
import io.swagger.annotations.Api;
9-
import lombok.AllArgsConstructor;
10-
import lombok.RequiredArgsConstructor;
119
import org.springframework.beans.factory.annotation.Autowired;
1210
import org.springframework.http.HttpStatus;
1311
import org.springframework.http.ResponseEntity;
14-
import org.springframework.web.bind.annotation.*;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RestController;
1519

1620
import javax.validation.Valid;
17-
import javax.validation.constraints.NotNull;
18-
import java.util.List;
19-
import java.util.stream.Collectors;
2021

2122
@Api("places")
2223
@RestController
2324
@RequestMapping("places")
24-
@AllArgsConstructor
2525
public class PlaceController {
2626

27+
@Autowired
2728
private PlaceService service;
2829

2930
@PostMapping
@@ -44,7 +45,6 @@ public ResponseEntity findAll() {
4445
return ResponseEntity.ok(places);
4546
}
4647

47-
4848
@PutMapping("/{id}")
4949
public ResponseEntity alter(@PathVariable Long id, @RequestBody @Valid PlaceDTO placeDTO) {
5050
Place place = service.findById(id).orElseThrow(null);

src/test/java/br/com/clickbus/challenge/PlacesApplicationTests.java

-15
This file was deleted.

src/test/java/br/com/clickbus/challenge/contoller/PlaceControllerTest.java

+60-67
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99
import org.junit.jupiter.api.BeforeEach;
1010
import org.junit.jupiter.api.Test;
1111
import org.junit.jupiter.api.extension.ExtendWith;
12-
import org.mockito.InjectMocks;
13-
import org.mockito.Mock;
14-
import org.mockito.junit.jupiter.MockitoExtension;
1512
import org.springframework.beans.factory.annotation.Autowired;
1613
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
1714
import org.springframework.boot.test.mock.mockito.MockBean;
1815
import org.springframework.http.MediaType;
1916
import org.springframework.test.context.junit.jupiter.SpringExtension;
2017
import org.springframework.test.web.servlet.MockMvc;
21-
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2218

2319
import java.util.Arrays;
2420
import java.util.Collections;
@@ -40,120 +36,113 @@
4036
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
4137

4238

43-
@ExtendWith(MockitoExtension.class)
44-
//@WebMvcTest(PlaceController.class)
45-
class PlaceControllerTest {
39+
@ExtendWith(SpringExtension.class)
40+
@WebMvcTest(PlaceController.class)
41+
public class PlaceControllerTest {
4642

47-
//@Autowired
43+
@Autowired
4844
private MockMvc mockMvc;
4945

50-
@Mock
46+
@MockBean
5147
private PlaceService service;
5248

53-
@InjectMocks
54-
private PlaceController placeController;
55-
49+
@Autowired
5650
private ObjectMapper objectMapper;
5751

5852
private Place place;
5953

6054
@BeforeEach
6155
public void setUp() {
62-
objectMapper = new ObjectMapper();
6356
place = Place.of("Butanta", "bt", "Sao Paulo", "SP");
64-
mockMvc = MockMvcBuilders
65-
.standaloneSetup(placeController)
66-
//.addFilters(new CORSFilter())
67-
.build();
6857
}
6958

7059
@Test
71-
void whenFindAllPlacesThenReturnASimpleItem() throws Exception {
60+
public void whenFindAllPlacesThenReturnASimpleItem() throws Exception {
7261

7362
when(service.findAll()).thenReturn(Collections.singletonList(place));
7463

7564
mockMvc.perform(get("/places")
7665
.accept(MediaType.APPLICATION_JSON))
77-
.andExpect(status().isOk())
78-
.andDo(print())
79-
.andExpect(jsonPath("$", hasSize(1)))
80-
.andExpect(jsonPath("$[0].name", is("Butanta")))
81-
.andExpect(jsonPath("$[0].slug", is("bt")))
82-
.andExpect(jsonPath("$[0].city", is("Sao Paulo")))
83-
.andExpect(jsonPath("$[0].state", is("SP")))
84-
.andReturn().getResponse();
66+
.andExpect(status().isOk())
67+
.andDo(print())
68+
.andExpect(jsonPath("$", hasSize(1)))
69+
.andExpect(jsonPath("$[0].name", is("Butanta")))
70+
.andExpect(jsonPath("$[0].slug", is("bt")))
71+
.andExpect(jsonPath("$[0].city", is("Sao Paulo")))
72+
.andExpect(jsonPath("$[0].state", is("SP")))
73+
.andReturn().getResponse();
8574

8675

8776
verify(service, atLeastOnce()).findAll();
8877

8978
}
9079

9180
@Test
92-
void whenFindByIdThenReturnOk() throws Exception {
81+
public void whenFindByIdThenReturnOk() throws Exception {
9382
when(service.findById(1L)).thenReturn(Optional.of(place));
9483

9584
mockMvc.perform(get("/places/{id}", 1L)
9685
.accept(MediaType.APPLICATION_JSON))
97-
.andExpect(status().isOk())
98-
.andDo(print())
99-
.andExpect(jsonPath("$.name", is("Butanta")))
100-
.andExpect(jsonPath("$.slug", is("bt")))
101-
.andExpect(jsonPath("$.city", is("Sao Paulo")))
102-
.andExpect(jsonPath("$.state", is("SP")))
103-
.andReturn().getResponse();
86+
.andExpect(status().isOk())
87+
.andDo(print())
88+
.andExpect(jsonPath("$.name", is("Butanta")))
89+
.andExpect(jsonPath("$.slug", is("bt")))
90+
.andExpect(jsonPath("$.city", is("Sao Paulo")))
91+
.andExpect(jsonPath("$.state", is("SP")))
92+
.andReturn().getResponse();
10493

10594
verify(service, atLeastOnce()).findById(anyLong());
10695
}
10796

10897
@Test
109-
void whenFindByIdThenReturnNotFound() throws Exception {
98+
public void whenFindByIdThenReturnNotFound() throws Exception {
11099
when(service.findById(1L)).thenReturn(Optional.empty());
111100

112101
mockMvc.perform(get("/places/{id}", 1L)
113102
.accept(MediaType.APPLICATION_JSON))
114-
.andExpect(status().isNotFound())
115-
.andDo(print())
116-
.andReturn().getResponse();
103+
.andExpect(status().isNotFound())
104+
.andDo(print())
105+
.andReturn().getResponse();
117106

118107
verify(service, atLeastOnce()).findById(anyLong());
119108
}
120109

121110
@Test
122-
void whenFindByNameThenReturnOk() throws Exception {
111+
public void whenFindByNameThenReturnOk() throws Exception {
123112
when(service.findByName("Butanta")).thenReturn(Arrays.asList(place));
124113

125114
mockMvc.perform(get("/places/?name={name}", "Butanta")
126115
.accept(MediaType.APPLICATION_JSON))
127-
.andExpect(status().isOk())
128-
.andDo(print())
129-
.andExpect(jsonPath("$", hasSize(1)))
130-
.andExpect(jsonPath("$[0].name", is("Butanta")))
131-
.andExpect(jsonPath("$[0].slug", is("bt")))
132-
.andExpect(jsonPath("$[0].city", is("Sao Paulo")))
133-
.andExpect(jsonPath("$[0].state", is("SP")))
134-
.andReturn().getResponse();
116+
.andExpect(status().isOk())
117+
.andDo(print())
118+
.andExpect(jsonPath("$", hasSize(1)))
119+
.andExpect(jsonPath("$[0].name", is("Butanta")))
120+
.andExpect(jsonPath("$[0].slug", is("bt")))
121+
.andExpect(jsonPath("$[0].city", is("Sao Paulo")))
122+
.andExpect(jsonPath("$[0].state", is("SP")))
123+
.andReturn().getResponse();
135124

136125

137126
verify(service, atLeastOnce()).findByName(anyString());
138127
}
139128

140129
@Test
141-
void whenFindByNameThenReturnNotFound() throws Exception {
130+
public void whenFindByNameThenReturnNotFound() throws Exception {
142131

143132
when(service.findByName("Cotia")).thenReturn(Collections.emptyList());
144133

145134
mockMvc.perform(get("/places/?name={name}", "Cotia")
146135
.accept(MediaType.APPLICATION_JSON))
147-
.andExpect(status().isNotFound())
148-
.andDo(print())
149-
.andReturn().getResponse();
136+
.andExpect(status().isNotFound())
137+
.andDo(print())
138+
.andReturn().getResponse();
150139

151140

152141
verify(service, atLeastOnce()).findByName(anyString());
153142
}
154143

155144
@Test
156-
void whenSaveThenReturnCreated() throws Exception {
145+
public void whenSaveThenReturnCreated() throws Exception {
157146
when(service.save(any(Place.class))).thenReturn(place);
158147

159148
System.out.println(objectMapper.writeValueAsString(place));
@@ -162,39 +151,43 @@ void whenSaveThenReturnCreated() throws Exception {
162151
.content(objectMapper.writeValueAsString(place))
163152
.contentType(MediaType.APPLICATION_JSON)
164153
.accept(MediaType.APPLICATION_JSON))
165-
.andExpect(status().isCreated())
166-
.andDo(print())
167-
.andReturn().getResponse();
154+
.andExpect(status().isCreated())
155+
.andDo(print())
156+
.andReturn().getResponse();
168157
}
169158

170159
@Test
171-
void whenSaveInvalidThenReturnBadRequest() throws Exception {
160+
public void whenSaveInvalidThenReturnBadRequest() throws Exception {
172161
Place place = Place.of(null, "bt", "Sao Paulo", "SP");
162+
when(service.save(any(Place.class))).thenReturn(place);
173163

174164
mockMvc.perform(post("/places")
175165
.content(objectMapper.writeValueAsString(place))
176166
.contentType(MediaType.APPLICATION_JSON)
177167
.accept(MediaType.APPLICATION_JSON))
178-
.andExpect(status().isBadRequest())
179-
.andDo(print())
180-
.andReturn().getResponse();
168+
.andExpect(status().isBadRequest())
169+
.andDo(print())
170+
.andReturn().getResponse();
181171
}
182172

183173
@Test
184-
void whenEdiWithPlaceInvalidThenReturnBadRequest() throws Exception {
174+
public void whenEdiWithPlaceInvalidThenReturnBadRequest() throws Exception {
175+
when(service.findById(1L)).thenReturn(Optional.of(place));
176+
185177
Place place = Place.of(null, "bt", "Sao Paulo", "SP");
178+
when(service.alter(any(Place.class), any(PlaceDTO.class))).thenReturn(place);
186179

187180
mockMvc.perform(put("/places/{id}", "1")
188181
.content(objectMapper.writeValueAsString(place))
189182
.contentType(MediaType.APPLICATION_JSON)
190183
.accept(MediaType.APPLICATION_JSON))
191-
.andExpect(status().isBadRequest())
192-
.andDo(print())
193-
.andReturn().getResponse();
184+
.andExpect(status().isBadRequest())
185+
.andDo(print())
186+
.andReturn().getResponse();
194187
}
195188

196189
@Test
197-
void whenEdiWithPlaceThenReturnOk() throws Exception {
190+
public void whenEdiWithPlaceThenReturnOk() throws Exception {
198191
when(service.findById(1L)).thenReturn(Optional.of(place));
199192

200193
Place place = Place.of("Butanta", "bt", "Sao Paulo", "SP");
@@ -204,8 +197,8 @@ void whenEdiWithPlaceThenReturnOk() throws Exception {
204197
.content(objectMapper.writeValueAsString(place))
205198
.contentType(MediaType.APPLICATION_JSON)
206199
.accept(MediaType.APPLICATION_JSON))
207-
.andExpect(status().isOk())
208-
.andDo(print())
209-
.andReturn().getResponse();
200+
.andExpect(status().isOk())
201+
.andDo(print())
202+
.andReturn().getResponse();
210203
}
211204
}

0 commit comments

Comments
 (0)