使用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");