Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Autowired
- private MockMvc mockMvc;
- @Autowired
- private ObjectMapper mapper;
- @MockBean
- private PlayersService mockPlayersByCountryVOService;
- @Test
- void testGetPlayersByCountry() throws Exception {
- when(mockPlayersByCountryVOService.getAllPlayersByCountry("Bulgaria")).thenReturn(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream());
- var response = mockMvc.perform(get("/api/players?country=Bulgaria").accept(MediaType.APPLICATION_JSON));
- response.andExpect(status().isOk())
- .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
- .andDo(result -> log.info("HTTP RESPONSE: {}", result.getResponse().getContentAsString()))
- .andExpect(jsonPath("$.length()").value(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream().collect(Collectors.toList()).size()))
- .andExpect(jsonPath("$[*].country",
- Matchers.hasItems(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream().map(PlayersByCountry::getCountry).collect(Collectors.toList()).toArray())));
- var body = response.andReturn().getResponse().getContentAsString();
- TypeReference<List<PlayersByCountry>> listTypeReference = new TypeReference<List<PlayersByCountry>>() {
- };
- var list = mapper.readValue(body,listTypeReference);
- org.hamcrest.MatcherAssert.assertThat(list,
- Matchers.hasItems(MOCK_PLAYERS_GROUP_BY_COUNTRY.stream().map(players -> Matchers.samePropertyValuesAs(players,"country"))
- .collect(Collectors.toList()).toArray(new Matcher[]{})));
- }
Advertisement
Add Comment
Please, Sign In to add comment