1414import org .mockito .Mockito ;
1515import org .springframework .beans .factory .annotation .Autowired ;
1616import org .springframework .boot .cache .test .autoconfigure .AutoConfigureCache ;
17+ import org .springframework .boot .test .autoconfigure .json .AutoConfigureJsonTesters ;
18+ import org .springframework .boot .test .context .TestConfiguration ;
1719import org .springframework .boot .webmvc .test .autoconfigure .WebMvcTest ;
20+ import org .springframework .context .annotation .Bean ;
1821import org .springframework .http .HttpHeaders ;
1922import org .springframework .http .HttpStatus ;
2023import org .springframework .http .MediaType ;
3639@ DisplayName ("HTTP Methods on Controller" )
3740@ WebMvcTest (PlayersController .class )
3841@ AutoConfigureCache
42+ @ AutoConfigureJsonTesters
3943class PlayersControllerTests {
4044
4145 private static final String PATH = "/players" ;
@@ -49,6 +53,17 @@ class PlayersControllerTests {
4953 @ MockitoBean
5054 private PlayersRepository playersRepositoryMock ;
5155
56+ @ Autowired
57+ private ObjectMapper objectMapper ;
58+
59+ @ TestConfiguration
60+ static class ObjectMapperTestConfig {
61+ @ Bean
62+ public ObjectMapper objectMapper () {
63+ return new ObjectMapper ().findAndRegisterModules ();
64+ }
65+ }
66+
5267 /*
5368 * -------------------------------------------------------------------------
5469 * HTTP POST
@@ -67,7 +82,7 @@ void post_validPlayer_returnsCreated()
6782 PlayerDTO playerDTO = PlayerDTOFakes .createOneValid ();
6883 PlayerDTO savedPlayer = PlayerDTOFakes .createOneValid ();
6984 savedPlayer .setId (19L ); // Simulating auto-generated ID
70- String body = new ObjectMapper () .writeValueAsString (playerDTO );
85+ String body = objectMapper .writeValueAsString (playerDTO );
7186 Mockito
7287 .when (playersServiceMock .create (any (PlayerDTO .class )))
7388 .thenReturn (savedPlayer );
@@ -80,7 +95,6 @@ void post_validPlayer_returnsCreated()
8095 .perform (request )
8196 .andReturn ()
8297 .getResponse ();
83- response .setContentType ("application/json;charset=UTF-8" );
8498 // Assert
8599 verify (playersServiceMock , times (1 )).create (any (PlayerDTO .class ));
86100 assertThat (response .getStatus ()).isEqualTo (HttpStatus .CREATED .value ());
@@ -98,7 +112,7 @@ void post_invalidPlayer_returnsBadRequest()
98112 throws Exception {
99113 // Arrange
100114 PlayerDTO playerDTO = PlayerDTOFakes .createOneInvalid ();
101- String body = new ObjectMapper () .writeValueAsString (playerDTO );
115+ String body = objectMapper .writeValueAsString (playerDTO );
102116 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
103117 .post (PATH )
104118 .content (body )
@@ -123,7 +137,7 @@ void post_squadNumberExists_returnsConflict()
123137 throws Exception {
124138 // Arrange
125139 PlayerDTO playerDTO = PlayerDTOFakes .createOneValid ();
126- String body = new ObjectMapper () .writeValueAsString (playerDTO );
140+ String body = objectMapper .writeValueAsString (playerDTO );
127141 Mockito
128142 .when (playersServiceMock .create (any (PlayerDTO .class )))
129143 .thenReturn (null ); // Conflict: squad number already exists
@@ -159,7 +173,7 @@ void getById_playerExists_returnsOkWithPlayer()
159173 PlayerDTO playerDTO = PlayerDTOFakes .createOneForUpdate ();
160174 Long id = 1L ;
161175 Mockito
162- .when (playersServiceMock .retrieveById (1L ))
176+ .when (playersServiceMock .retrieveById (id ))
163177 .thenReturn (playerDTO );
164178 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
165179 .get (PATH + "/{id}" , id );
@@ -168,11 +182,11 @@ void getById_playerExists_returnsOkWithPlayer()
168182 .perform (request )
169183 .andReturn ()
170184 .getResponse ();
171- response .setContentType ("application/json;charset=UTF-8" );
172185 String content = response .getContentAsString ();
173- PlayerDTO result = new ObjectMapper () .readValue (content , PlayerDTO .class );
186+ PlayerDTO result = objectMapper .readValue (content , PlayerDTO .class );
174187 // Assert
175- verify (playersServiceMock , times (1 )).retrieveById (1L );
188+ assertThat (response .getContentType ()).contains ("application/json" );
189+ verify (playersServiceMock , times (1 )).retrieveById (id );
176190 assertThat (response .getStatus ()).isEqualTo (HttpStatus .OK .value ());
177191 assertThat (result ).usingRecursiveComparison ().isEqualTo (playerDTO );
178192 }
@@ -222,11 +236,11 @@ void getAll_playersExist_returnsOkWithAllPlayers()
222236 .perform (request )
223237 .andReturn ()
224238 .getResponse ();
225- response .setContentType ("application/json;charset=UTF-8" );
226239 String content = response .getContentAsString ();
227- List <PlayerDTO > result = new ObjectMapper () .readValue (content , new TypeReference <List <PlayerDTO >>() {
240+ List <PlayerDTO > result = objectMapper .readValue (content , new TypeReference <List <PlayerDTO >>() {
228241 });
229242 // Assert
243+ assertThat (response .getContentType ()).contains ("application/json" );
230244 verify (playersServiceMock , times (1 )).retrieveAll ();
231245 assertThat (response .getStatus ()).isEqualTo (HttpStatus .OK .value ());
232246 assertThat (result ).hasSize (26 );
@@ -255,11 +269,11 @@ void searchByLeague_matchingPlayersExist_returnsOkWithList()
255269 .perform (request )
256270 .andReturn ()
257271 .getResponse ();
258- response .setContentType ("application/json;charset=UTF-8" );
259272 String content = response .getContentAsString ();
260- List <PlayerDTO > result = new ObjectMapper () .readValue (content , new TypeReference <List <PlayerDTO >>() {
273+ List <PlayerDTO > result = objectMapper .readValue (content , new TypeReference <List <PlayerDTO >>() {
261274 });
262275 // Assert
276+ assertThat (response .getContentType ()).contains ("application/json" );
263277 verify (playersServiceMock , times (1 )).searchByLeague (any ());
264278 assertThat (response .getStatus ()).isEqualTo (HttpStatus .OK .value ());
265279 assertThat (result ).hasSize (7 );
@@ -285,11 +299,11 @@ void searchByLeague_noMatches_returnsOkWithEmptyList()
285299 .perform (request )
286300 .andReturn ()
287301 .getResponse ();
288- response .setContentType ("application/json;charset=UTF-8" );
289302 String content = response .getContentAsString ();
290- List <PlayerDTO > result = new ObjectMapper () .readValue (content , new TypeReference <List <PlayerDTO >>() {
303+ List <PlayerDTO > result = objectMapper .readValue (content , new TypeReference <List <PlayerDTO >>() {
291304 });
292305 // Assert
306+ assertThat (response .getContentType ()).contains ("application/json" );
293307 verify (playersServiceMock , times (1 )).searchByLeague (any ());
294308 assertThat (response .getStatus ()).isEqualTo (HttpStatus .OK .value ());
295309 assertThat (result ).isEmpty ();
@@ -315,10 +329,10 @@ void searchBySquadNumber_playerExists_returnsOkWithPlayer()
315329 .perform (request )
316330 .andReturn ()
317331 .getResponse ();
318- response .setContentType ("application/json;charset=UTF-8" );
319332 String content = response .getContentAsString ();
320- PlayerDTO result = new ObjectMapper () .readValue (content , PlayerDTO .class );
333+ PlayerDTO result = objectMapper .readValue (content , PlayerDTO .class );
321334 // Assert
335+ assertThat (response .getContentType ()).contains ("application/json" );
322336 verify (playersServiceMock , times (1 )).searchBySquadNumber (10 );
323337 assertThat (response .getStatus ()).isEqualTo (HttpStatus .OK .value ());
324338 assertThat (result ).usingRecursiveComparison ().isEqualTo (playerDTO );
@@ -367,7 +381,7 @@ void put_playerExists_returnsNoContent()
367381 PlayerDTO playerDTO = PlayerDTOFakes .createOneValid ();
368382 playerDTO .setId (1L ); // Set ID for update operation
369383 Long id = playerDTO .getId ();
370- String body = new ObjectMapper () .writeValueAsString (playerDTO );
384+ String body = objectMapper .writeValueAsString (playerDTO );
371385 Mockito
372386 .when (playersServiceMock .update (any (PlayerDTO .class )))
373387 .thenReturn (true );
@@ -397,7 +411,7 @@ void put_playerNotFound_returnsNotFound()
397411 PlayerDTO playerDTO = PlayerDTOFakes .createOneValid ();
398412 playerDTO .setId (999L ); // Set ID for update operation
399413 Long id = playerDTO .getId ();
400- String body = new ObjectMapper () .writeValueAsString (playerDTO );
414+ String body = objectMapper .writeValueAsString (playerDTO );
401415 Mockito
402416 .when (playersServiceMock .update (any (PlayerDTO .class )))
403417 .thenReturn (false );
@@ -425,8 +439,8 @@ void put_invalidPlayer_returnsBadRequest()
425439 throws Exception {
426440 // Arrange
427441 PlayerDTO playerDTO = PlayerDTOFakes .createOneInvalid ();
428- Long id = 1L ;
429- String body = new ObjectMapper () .writeValueAsString (playerDTO );
442+ Long id = playerDTO . getId () ;
443+ String body = objectMapper .writeValueAsString (playerDTO );
430444 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
431445 .put (PATH + "/{id}" , id )
432446 .content (body )
@@ -453,7 +467,7 @@ void put_idMismatch_returnsBadRequest()
453467 PlayerDTO playerDTO = PlayerDTOFakes .createOneValid ();
454468 playerDTO .setId (999L ); // Body has different ID
455469 Long pathId = 1L ; // Path has different ID
456- String body = new ObjectMapper () .writeValueAsString (playerDTO );
470+ String body = objectMapper .writeValueAsString (playerDTO );
457471 MockHttpServletRequestBuilder request = MockMvcRequestBuilders
458472 .put (PATH + "/{id}" , pathId )
459473 .content (body )
0 commit comments