【发布时间】:2013-08-22 13:50:21
【问题描述】:
我有简单的集成测试
@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(?);
}
在最后一行我想将响应正文中收到的字符串与预期的字符串进行比较
作为回应,我得到:
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
用 content()、body() 尝试了一些技巧,但没有奏效。
【问题讨论】:
-
作为建议,不应为
"Username already taken"之类的内容返回 400 状态代码。这应该更像是 409 冲突。 -
Thanx - 此测试的目标是指定此类内容。
标签: java spring mocking spring-test-mvc