Tag: springtestingmvc

如何使用mockMvc检查响应正文中的string

我有简单的集成testing @Test public void shouldReturnErrorMessageToAdminWhenCreatingUserWithUsedUserName() throws Exception { mockMvc.perform(post("/api/users").header("Authorization", base64ForTestUser).contentType(MediaType.APPLICATION_JSON) .content("{\"userName\":\"testUserDetails\",\"firstName\":\"xxx\",\"lastName\":\"xxx\",\"password\":\"xxx\"}")) .andDo(print()) .andExpect(status().isBadRequest()) .andExpect(?); } 在最后一行,我想比较收到的响应正文string和预期的string 作为回应,我得到: MockHttpServletResponse: Status = 400 Error message = null Headers = {Content-Type=[application/json]} Content type = application/json Body = "Username already taken" Forwarded URL = null Redirected URL = null 尝试了一些与内容(),身体()的技巧,但没有任何工作。