Using Spring's mockMvc, how do I check if the returned data contains part of a string?
Another way you can get to the String respone of a mockMVC requst so you can compare or manipulate it in other ways is as follows:
MvcResult result = mockMvc.perform(get("/api/users/" + id)) .andExpect(status().isOk()) .andReturn();String stringResult = result.getResponse().getContentAsString();boolean doesContain = stringResult.contains("{\"id\":\"" + id + "\"}");
You could also wrap the whole thing in an assertTrue while still using String methods:
assertTrue(mockMvc.perform(get("/api/users/" + id)) .andExpect(status().isOk()) .andReturn() .getResponse() .getContentAsString() .contains("{\"id\":\"" + id + "\"}");
I prefer the approved answer, just thought I would submit this as another alternative.