How to check String in response body with mockMvc
To check a string in the response body with
MockMvc, you can use the
andExpect() method of the
Here is an example of how to use
MockMvcResultMatchers.andExpect() to check a string in the response body:
mockMvc.perform(get("/users")) .andExpect(status().isOk()) .andExpect(content().string("user1, user2, user3"));
In this example, the
get() method of
MockMvcRequestBuilders is used to send a GET request to the
/users URL, and the
andExpect() method is used to assert that the response has a 200 OK status and that the response body contains the string "user1, user2, user3".
You can also use the
MockMvcResultMatchers.jsonPath() method to check a specific value in the response body using JSONPath. For example:
mockMvc.perform(get("/users")) .andExpect(status().isOk()) .andExpect(jsonPath("$.users.name").value("user1"));
This example checks that the first element in the "users" array has a "name" property with the value "user1".
I hope this helps. Let me know if you have any questions.