9
9
import org .junit .jupiter .api .BeforeEach ;
10
10
import org .junit .jupiter .api .Test ;
11
11
import org .junit .jupiter .api .extension .ExtendWith ;
12
- import org .mockito .InjectMocks ;
13
- import org .mockito .Mock ;
14
- import org .mockito .junit .jupiter .MockitoExtension ;
15
12
import org .springframework .beans .factory .annotation .Autowired ;
16
13
import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
17
14
import org .springframework .boot .test .mock .mockito .MockBean ;
18
15
import org .springframework .http .MediaType ;
19
16
import org .springframework .test .context .junit .jupiter .SpringExtension ;
20
17
import org .springframework .test .web .servlet .MockMvc ;
21
- import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
22
18
23
19
import java .util .Arrays ;
24
20
import java .util .Collections ;
40
36
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
41
37
42
38
43
- @ ExtendWith (MockitoExtension .class )
44
- // @WebMvcTest(PlaceController.class)
45
- class PlaceControllerTest {
39
+ @ ExtendWith (SpringExtension .class )
40
+ @ WebMvcTest (PlaceController .class )
41
+ public class PlaceControllerTest {
46
42
47
- // @Autowired
43
+ @ Autowired
48
44
private MockMvc mockMvc ;
49
45
50
- @ Mock
46
+ @ MockBean
51
47
private PlaceService service ;
52
48
53
- @ InjectMocks
54
- private PlaceController placeController ;
55
-
49
+ @ Autowired
56
50
private ObjectMapper objectMapper ;
57
51
58
52
private Place place ;
59
53
60
54
@ BeforeEach
61
55
public void setUp () {
62
- objectMapper = new ObjectMapper ();
63
56
place = Place .of ("Butanta" , "bt" , "Sao Paulo" , "SP" );
64
- mockMvc = MockMvcBuilders
65
- .standaloneSetup (placeController )
66
- //.addFilters(new CORSFilter())
67
- .build ();
68
57
}
69
58
70
59
@ Test
71
- void whenFindAllPlacesThenReturnASimpleItem () throws Exception {
60
+ public void whenFindAllPlacesThenReturnASimpleItem () throws Exception {
72
61
73
62
when (service .findAll ()).thenReturn (Collections .singletonList (place ));
74
63
75
64
mockMvc .perform (get ("/places" )
76
65
.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 ();
85
74
86
75
87
76
verify (service , atLeastOnce ()).findAll ();
88
77
89
78
}
90
79
91
80
@ Test
92
- void whenFindByIdThenReturnOk () throws Exception {
81
+ public void whenFindByIdThenReturnOk () throws Exception {
93
82
when (service .findById (1L )).thenReturn (Optional .of (place ));
94
83
95
84
mockMvc .perform (get ("/places/{id}" , 1L )
96
85
.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 ();
104
93
105
94
verify (service , atLeastOnce ()).findById (anyLong ());
106
95
}
107
96
108
97
@ Test
109
- void whenFindByIdThenReturnNotFound () throws Exception {
98
+ public void whenFindByIdThenReturnNotFound () throws Exception {
110
99
when (service .findById (1L )).thenReturn (Optional .empty ());
111
100
112
101
mockMvc .perform (get ("/places/{id}" , 1L )
113
102
.accept (MediaType .APPLICATION_JSON ))
114
- .andExpect (status ().isNotFound ())
115
- .andDo (print ())
116
- .andReturn ().getResponse ();
103
+ .andExpect (status ().isNotFound ())
104
+ .andDo (print ())
105
+ .andReturn ().getResponse ();
117
106
118
107
verify (service , atLeastOnce ()).findById (anyLong ());
119
108
}
120
109
121
110
@ Test
122
- void whenFindByNameThenReturnOk () throws Exception {
111
+ public void whenFindByNameThenReturnOk () throws Exception {
123
112
when (service .findByName ("Butanta" )).thenReturn (Arrays .asList (place ));
124
113
125
114
mockMvc .perform (get ("/places/?name={name}" , "Butanta" )
126
115
.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 ();
135
124
136
125
137
126
verify (service , atLeastOnce ()).findByName (anyString ());
138
127
}
139
128
140
129
@ Test
141
- void whenFindByNameThenReturnNotFound () throws Exception {
130
+ public void whenFindByNameThenReturnNotFound () throws Exception {
142
131
143
132
when (service .findByName ("Cotia" )).thenReturn (Collections .emptyList ());
144
133
145
134
mockMvc .perform (get ("/places/?name={name}" , "Cotia" )
146
135
.accept (MediaType .APPLICATION_JSON ))
147
- .andExpect (status ().isNotFound ())
148
- .andDo (print ())
149
- .andReturn ().getResponse ();
136
+ .andExpect (status ().isNotFound ())
137
+ .andDo (print ())
138
+ .andReturn ().getResponse ();
150
139
151
140
152
141
verify (service , atLeastOnce ()).findByName (anyString ());
153
142
}
154
143
155
144
@ Test
156
- void whenSaveThenReturnCreated () throws Exception {
145
+ public void whenSaveThenReturnCreated () throws Exception {
157
146
when (service .save (any (Place .class ))).thenReturn (place );
158
147
159
148
System .out .println (objectMapper .writeValueAsString (place ));
@@ -162,39 +151,43 @@ void whenSaveThenReturnCreated() throws Exception {
162
151
.content (objectMapper .writeValueAsString (place ))
163
152
.contentType (MediaType .APPLICATION_JSON )
164
153
.accept (MediaType .APPLICATION_JSON ))
165
- .andExpect (status ().isCreated ())
166
- .andDo (print ())
167
- .andReturn ().getResponse ();
154
+ .andExpect (status ().isCreated ())
155
+ .andDo (print ())
156
+ .andReturn ().getResponse ();
168
157
}
169
158
170
159
@ Test
171
- void whenSaveInvalidThenReturnBadRequest () throws Exception {
160
+ public void whenSaveInvalidThenReturnBadRequest () throws Exception {
172
161
Place place = Place .of (null , "bt" , "Sao Paulo" , "SP" );
162
+ when (service .save (any (Place .class ))).thenReturn (place );
173
163
174
164
mockMvc .perform (post ("/places" )
175
165
.content (objectMapper .writeValueAsString (place ))
176
166
.contentType (MediaType .APPLICATION_JSON )
177
167
.accept (MediaType .APPLICATION_JSON ))
178
- .andExpect (status ().isBadRequest ())
179
- .andDo (print ())
180
- .andReturn ().getResponse ();
168
+ .andExpect (status ().isBadRequest ())
169
+ .andDo (print ())
170
+ .andReturn ().getResponse ();
181
171
}
182
172
183
173
@ Test
184
- void whenEdiWithPlaceInvalidThenReturnBadRequest () throws Exception {
174
+ public void whenEdiWithPlaceInvalidThenReturnBadRequest () throws Exception {
175
+ when (service .findById (1L )).thenReturn (Optional .of (place ));
176
+
185
177
Place place = Place .of (null , "bt" , "Sao Paulo" , "SP" );
178
+ when (service .alter (any (Place .class ), any (PlaceDTO .class ))).thenReturn (place );
186
179
187
180
mockMvc .perform (put ("/places/{id}" , "1" )
188
181
.content (objectMapper .writeValueAsString (place ))
189
182
.contentType (MediaType .APPLICATION_JSON )
190
183
.accept (MediaType .APPLICATION_JSON ))
191
- .andExpect (status ().isBadRequest ())
192
- .andDo (print ())
193
- .andReturn ().getResponse ();
184
+ .andExpect (status ().isBadRequest ())
185
+ .andDo (print ())
186
+ .andReturn ().getResponse ();
194
187
}
195
188
196
189
@ Test
197
- void whenEdiWithPlaceThenReturnOk () throws Exception {
190
+ public void whenEdiWithPlaceThenReturnOk () throws Exception {
198
191
when (service .findById (1L )).thenReturn (Optional .of (place ));
199
192
200
193
Place place = Place .of ("Butanta" , "bt" , "Sao Paulo" , "SP" );
@@ -204,8 +197,8 @@ void whenEdiWithPlaceThenReturnOk() throws Exception {
204
197
.content (objectMapper .writeValueAsString (place ))
205
198
.contentType (MediaType .APPLICATION_JSON )
206
199
.accept (MediaType .APPLICATION_JSON ))
207
- .andExpect (status ().isOk ())
208
- .andDo (print ())
209
- .andReturn ().getResponse ();
200
+ .andExpect (status ().isOk ())
201
+ .andDo (print ())
202
+ .andReturn ().getResponse ();
210
203
}
211
204
}
0 commit comments