mockMvc-测试错误消息
是否有人有任何提示,或者有人知道我如何测试HTTP响应对象返回的“错误消息”?
@Autowiredprivate WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
}
响应:
MockHttpServletResponse:              Status = 200
       Error message = null
             Headers = {Content-Type=[application/json;charset=UTF-8]}
        Content type = application/json;charset=UTF-8
回答:
您可以使用status.reason()方法。
例如:
     @Test     public void loginWithBadCredentials() {
        this.mockMvc.perform(
                post("/rest/login")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content("{\"username\": \"baduser\", \"password\": \"invalidPassword\"}")
                )
                .andDo(MockMvcResultHandlers.print())
                .andExpect(status().isUnauthorized())
                .andExpect(status().reason(containsString("Bad credentials")))
                .andExpect(unauthenticated());
    }
    MockHttpServletResponse:
                  Status = 401
           Error message = Authentication Failed: Bad credentials
            Content type = null
                    Body = 
           Forwarded URL = null
          Redirected URL = null
                 Cookies = []
以上是 mockMvc-测试错误消息 的全部内容, 来源链接: utcz.com/qa/420201.html






