Bangeev

Untitled

Aug 15th, 2022
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1.     @Autowired
  2.         private MockMvc mockMvc;
  3.     @Autowired
  4.     private ObjectMapper mapper;
  5.     @MockBean
  6.     private PlayersService mockPlayersByCountryVOService;
  7.  
  8.     @Test
  9.     void testGetPlayersByCountry() throws Exception {
  10.         when(mockPlayersByCountryVOService.getAllPlayersByCountry("Bulgaria")).thenReturn(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream());
  11.  
  12.         var response = mockMvc.perform(get("/api/players?country=Bulgaria").accept(MediaType.APPLICATION_JSON));
  13.  
  14.         response.andExpect(status().isOk())
  15.                 .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
  16.                 .andDo(result -> log.info("HTTP RESPONSE: {}", result.getResponse().getContentAsString()))
  17.                 .andExpect(jsonPath("$.length()").value(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream().collect(Collectors.toList()).size()))
  18.                 .andExpect(jsonPath("$[*].country",
  19.                         Matchers.hasItems(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream().map(PlayersByCountry::getCountry).collect(Collectors.toList()).toArray())));
  20.  
  21.         var body = response.andReturn().getResponse().getContentAsString();
  22.         TypeReference<List<PlayersByCountry>> listTypeReference = new TypeReference<List<PlayersByCountry>>() {
  23.         };
  24.  
  25.         var list = mapper.readValue(body,listTypeReference);
  26.         org.hamcrest.MatcherAssert.assertThat(list,
  27.                 Matchers.hasItems(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream().map(players -> Matchers.samePropertyValuesAs(players,"country"))
  28.                         .collect(Collectors.toList()).toArray(new Matcher[]{})));
  29.  
  30.     }
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment