I want to retrieve a value from json response in order to use in the rest of my test case, here's what I'm doing now:
MvcResult mvcResult = super.mockMvc.perform(get("url").accept(MediaType.APPLICATION_JSON).headers(basicAuthHeaders()))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id", is(6))).andReturn();
String responseAsString = mvcResult.getResponse().getContentAsString();
ObjectMapper objectMapper = new ObjectMapper(); // com.fasterxml.jackson.databind.ObjectMapper
MyResponse myResponse = objectMapper.readValue(responseAsString, MyResponse.class);
if(myResponse.getName().equals("name")) {
//
//
}
I'm wondering is there a more elegant way to retrieve a value directly from MvcResult
as with the case of jsonPath
for matching?
I've found a more elegant way using
JsonPath
of Jayway: