3131import ar .com .nanotaboada .java .samples .spring .boot .services .BooksService ;
3232import ar .com .nanotaboada .java .samples .spring .boot .test .BookDTOsBuilder ;
3333
34- @ DisplayName ("HTTP Verbs on Controller" )
34+ @ DisplayName ("HTTP Methods on Controller" )
3535@ WebMvcTest (BooksController .class )
3636class BooksControllerTests {
3737
38+ private static final String PATH = "/books" ;
39+
3840 @ Autowired
3941 private MockMvc mockMvc ;
4042
@@ -49,7 +51,7 @@ class BooksControllerTests {
4951 * ----------------------------------------------------------------------------------------- */
5052
5153 @ Test
52- void givenHttpPostVerb_whenRequestBodyContainsExistingValidBook_thenShouldReturnStatusConflict ()
54+ void givenPost_whenRequestBodyContainsExistingValidBook_thenShouldReturnStatusConflict ()
5355 throws Exception {
5456 // Arrange
5557 BookDTO bookDTO = BookDTOsBuilder .buildOneInvalid ();
@@ -58,7 +60,7 @@ void givenHttpPostVerb_whenRequestBodyContainsExistingValidBook_thenShouldReturn
5860 .when (service .retrieveByIsbn (anyString ()))
5961 .thenReturn (bookDTO ); // Existing
6062 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
61- .post ("/book" )
63+ .post (PATH )
6264 .content (content )
6365 .contentType (MediaType .APPLICATION_JSON );
6466 // Act
@@ -72,7 +74,7 @@ void givenHttpPostVerb_whenRequestBodyContainsExistingValidBook_thenShouldReturn
7274 }
7375
7476 @ Test
75- void givenHttpPostVerb_whenRequestBodyContainsNewValidBook_thenShouldReturnStatusCreatedAndLocationHeader ()
77+ void givenPost_whenRequestBodyContainsNewValidBook_thenShouldReturnStatusCreatedAndLocationHeader ()
7678 throws Exception {
7779 // Arrange
7880 BookDTO bookDTO = BookDTOsBuilder .buildOneInvalid ();
@@ -84,7 +86,7 @@ void givenHttpPostVerb_whenRequestBodyContainsNewValidBook_thenShouldReturnStatu
8486 .when (service .create (any (BookDTO .class )))
8587 .thenReturn (true );
8688 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
87- .post ("/book" )
89+ .post (PATH )
8890 .content (content )
8991 .contentType (MediaType .APPLICATION_JSON );
9092 // Act
@@ -96,13 +98,13 @@ void givenHttpPostVerb_whenRequestBodyContainsNewValidBook_thenShouldReturnStatu
9698 // Assert
9799 assertThat (response .getStatus ()).isEqualTo (HttpStatus .CREATED .value ());
98100 assertThat (response .getHeader (HttpHeaders .LOCATION )).isNotNull ();
99- assertThat (response .getHeader (HttpHeaders .LOCATION )).contains ("/book /" + bookDTO .getIsbn ());
101+ assertThat (response .getHeader (HttpHeaders .LOCATION )).contains (PATH + " /" + bookDTO .getIsbn ());
100102 verify (service , times (1 )).retrieveByIsbn (anyString ());
101103 verify (service , times (1 )).create (any (BookDTO .class ));
102104 }
103105
104106 @ Test
105- void givenHttpPostVerb_whenRequestBodyContainsInvalidBook_thenShouldReturnStatusBadRequest ()
107+ void givenPost_whenRequestBodyContainsInvalidBook_thenShouldReturnStatusBadRequest ()
106108 throws Exception {
107109 // Arrange
108110 BookDTO bookDTO = BookDTOsBuilder .buildOneInvalid ();
@@ -114,7 +116,7 @@ void givenHttpPostVerb_whenRequestBodyContainsInvalidBook_thenShouldReturnStatus
114116 .when (service .create (any (BookDTO .class )))
115117 .thenReturn (false );
116118 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
117- .post ("/book" )
119+ .post (PATH )
118120 .content (content )
119121 .contentType (MediaType .APPLICATION_JSON );
120122 // Act
@@ -133,15 +135,15 @@ void givenHttpPostVerb_whenRequestBodyContainsInvalidBook_thenShouldReturnStatus
133135 * ----------------------------------------------------------------------------------------- */
134136
135137 @ Test
136- void givenHttpGetVerb_whenRequestParameterIdentifiesExistingBook_thenShouldReturnStatusOkAndTheBook ()
138+ void givenGetByIsbn_whenRequestParameterIdentifiesExistingBook_thenShouldReturnStatusOkAndTheBook ()
137139 throws Exception {
138140 // Arrange
139141 BookDTO bookDTO = BookDTOsBuilder .buildOneValid ();
140142 Mockito
141143 .when (service .retrieveByIsbn (anyString ()))
142144 .thenReturn (bookDTO ); // Existing
143145 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
144- .get ("/book /{isbn}" , bookDTO .getIsbn ());
146+ .get (PATH + " /{isbn}" , bookDTO .getIsbn ());
145147 // Act
146148 MockHttpServletResponse response = mockMvc
147149 .perform (request )
@@ -157,15 +159,15 @@ void givenHttpGetVerb_whenRequestParameterIdentifiesExistingBook_thenShouldRetur
157159 }
158160
159161 @ Test
160- void givenHttpGetVerb_whenRequestParameterDoesNotIdentifyAnExistingBook_thenShouldReturnStatusNotFound ()
162+ void givenGetByIsbn_whenRequestParameterDoesNotIdentifyAnExistingBook_thenShouldReturnStatusNotFound ()
161163 throws Exception {
162164 // Arrange
163165 String isbn = "9781484242216" ;
164166 Mockito
165167 .when (service .retrieveByIsbn (anyString ()))
166168 .thenReturn (null ); // New
167169 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
168- .get ("/book /{isbn}" , isbn );
170+ .get (PATH + " /{isbn}" , isbn );
169171 // Act
170172 MockHttpServletResponse response = mockMvc
171173 .perform (request )
@@ -177,15 +179,15 @@ void givenHttpGetVerb_whenRequestParameterDoesNotIdentifyAnExistingBook_thenShou
177179 }
178180
179181 @ Test
180- void givenHttpGetVerb_whenRequestPathIsBooks_thenShouldReturnStatusOkAndCollectionOfBooks ()
182+ void givenGetAll_whenRequestPathIsBooks_thenShouldReturnStatusOkAndCollectionOfBooks ()
181183 throws Exception {
182184 // Arrange
183185 List <BookDTO > expected = BookDTOsBuilder .buildManyValid ();
184186 Mockito
185187 .when (service .retrieveAll ())
186188 .thenReturn (expected );
187189 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
188- .get ("/books" );
190+ .get (PATH );
189191 // Act
190192 MockHttpServletResponse response = mockMvc
191193 .perform (request )
@@ -205,7 +207,7 @@ void givenHttpGetVerb_whenRequestPathIsBooks_thenShouldReturnStatusOkAndCollecti
205207 * ----------------------------------------------------------------------------------------- */
206208
207209 @ Test
208- void givenHttpPutVerb_whenRequestBodyContainsExistingValidBook_thenShouldReturnStatusNoContent ()
210+ void givenPut_whenRequestBodyContainsExistingValidBook_thenShouldReturnStatusNoContent ()
209211 throws Exception {
210212 // Arrange
211213 BookDTO bookDTO = BookDTOsBuilder .buildOneValid ();
@@ -217,7 +219,7 @@ void givenHttpPutVerb_whenRequestBodyContainsExistingValidBook_thenShouldReturnS
217219 .when (service .update (any (BookDTO .class )))
218220 .thenReturn (true );
219221 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
220- .put ("/book" )
222+ .put (PATH )
221223 .content (content )
222224 .contentType (MediaType .APPLICATION_JSON );
223225 // Act
@@ -232,7 +234,7 @@ void givenHttpPutVerb_whenRequestBodyContainsExistingValidBook_thenShouldReturnS
232234 }
233235
234236 @ Test
235- void givenHttpPutVerb_whenRequestBodyContainsExistingInvalidBook_thenShouldReturnStatusBadRequest ()
237+ void givenPut_whenRequestBodyContainsExistingInvalidBook_thenShouldReturnStatusBadRequest ()
236238 throws Exception {
237239 // Arrange
238240 BookDTO bookDTO = BookDTOsBuilder .buildOneInvalid ();
@@ -244,7 +246,7 @@ void givenHttpPutVerb_whenRequestBodyContainsExistingInvalidBook_thenShouldRetur
244246 .when (service .update (any (BookDTO .class )))
245247 .thenReturn (false );
246248 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
247- .put ("/book" )
249+ .put (PATH )
248250 .content (content )
249251 .contentType (MediaType .APPLICATION_JSON );
250252 // Act
@@ -259,7 +261,7 @@ void givenHttpPutVerb_whenRequestBodyContainsExistingInvalidBook_thenShouldRetur
259261 }
260262
261263 @ Test
262- void givenHttpPutVerb_whenRequestBodyContainsNewValidBook_thenShouldReturnStatusNotFound ()
264+ void givenPut_whenRequestBodyContainsNewValidBook_thenShouldReturnStatusNotFound ()
263265 throws Exception {
264266 // Arrange
265267 BookDTO bookDTO = BookDTOsBuilder .buildOneValid ();
@@ -268,7 +270,7 @@ void givenHttpPutVerb_whenRequestBodyContainsNewValidBook_thenShouldReturnStatus
268270 .when (service .retrieveByIsbn (anyString ()))
269271 .thenReturn (null ); // New
270272 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
271- .put ("/book" )
273+ .put (PATH )
272274 .content (content )
273275 .contentType (MediaType .APPLICATION_JSON );
274276 // Act
@@ -286,7 +288,7 @@ void givenHttpPutVerb_whenRequestBodyContainsNewValidBook_thenShouldReturnStatus
286288 * ----------------------------------------------------------------------------------------- */
287289
288290 @ Test
289- void givenHttpDeleteVerb_whenRequestBodyContainsExistingValidBook_thenShouldReturnStatusNoContent ()
291+ void givenDelete_whenRequestBodyContainsExistingValidBook_thenShouldReturnStatusNoContent ()
290292 throws Exception {
291293 // Arrange
292294 BookDTO bookDTO = BookDTOsBuilder .buildOneValid ();
@@ -297,7 +299,7 @@ void givenHttpDeleteVerb_whenRequestBodyContainsExistingValidBook_thenShouldRetu
297299 .when (service .delete (anyString ()))
298300 .thenReturn (true );
299301 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
300- .delete ("/book /{isbn}" , bookDTO .getIsbn ());
302+ .delete (PATH + " /{isbn}" , bookDTO .getIsbn ());
301303 // Act
302304 MockHttpServletResponse response = mockMvc
303305 .perform (request )
@@ -310,7 +312,7 @@ void givenHttpDeleteVerb_whenRequestBodyContainsExistingValidBook_thenShouldRetu
310312 }
311313
312314 @ Test
313- void givenHttpDeleteVerb_whenRequestBodyContainsExistingInvalidBook_thenShouldReturnStatusBadRequest ()
315+ void givenDelete_whenRequestBodyContainsExistingInvalidBook_thenShouldReturnStatusBadRequest ()
314316 throws Exception {
315317 // Arrange
316318 BookDTO bookDTO = BookDTOsBuilder .buildOneInvalid ();
@@ -321,7 +323,7 @@ void givenHttpDeleteVerb_whenRequestBodyContainsExistingInvalidBook_thenShouldRe
321323 .when (service .delete (anyString ()))
322324 .thenReturn (false );
323325 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
324- .delete ("/book /{isbn}" , bookDTO .getIsbn ());
326+ .delete (PATH + " /{isbn}" , bookDTO .getIsbn ());
325327 // Act
326328 MockHttpServletResponse response = mockMvc
327329 .perform (request )
@@ -334,15 +336,15 @@ void givenHttpDeleteVerb_whenRequestBodyContainsExistingInvalidBook_thenShouldRe
334336 }
335337
336338 @ Test
337- void givenHttpDeleteVerb_whenRequestBodyContainsNewValidBook_thenShouldReturnStatusNotFound ()
339+ void givenDelete_whenRequestBodyContainsNewValidBook_thenShouldReturnStatusNotFound ()
338340 throws Exception {
339341 // Arrange
340342 BookDTO bookDTO = BookDTOsBuilder .buildOneValid ();
341343 Mockito
342344 .when (service .retrieveByIsbn (anyString ()))
343345 .thenReturn (null ); // New
344346 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
345- .delete ("/book /{isbn}" , bookDTO .getIsbn ());
347+ .delete (PATH + " /{isbn}" , bookDTO .getIsbn ());
346348 // Act
347349 MockHttpServletResponse response = mockMvc
348350 .perform (request )
0 commit comments