MockMvc引发异常时如何声明状态代码

时间:2019-06-28 10:10:08

标签: mockmvc assertj

使用MockMvc时,如果引发异常,则expect(matcher)结果将被忽略。

在这种情况下如何声明状态码?

示例代码如下:

@Test
public void testStatusCode() {
MockHttpServletRequestBuilder request = MockMvcRequestBuilders
                .request(HttpMethod.POST, "XXXX")
                .content("{}");

Throwable throwable = catchThrowable(() -> mvc.perform(request)
                    // status() is ignored 
                    .andExpect(status().isUnauthorized()));

assertThat(throwable)
     .isInstanceOf(ServiceException.class)
     .hasMessage("Auth header is not present");

0 个答案:

没有答案