How to prevent NestedServletException when testing Spring endpoints?

前端 未结 3 1301
耶瑟儿~
耶瑟儿~ 2021-02-05 13:11

I am trying to test the security configuration of some of my endpoints which are secured with @PreAuthorize(#oauth2.hasScope(\'scope\'). When acces

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 13:45

    I had a similar case and suppressed the NestedServletException by using @Test(expected = NestedServletException.class) and then I was able to get hold of the MvcResult and do further asserts on it as in other tests like:

    // then
    MvcResult result = resultActions.andExpect(status().isServiceUnavailable()).andReturn();
    String message = result.getResponse().getContentAsString();
    assertThat(message).contains("ABC");
    assertThat(result.getResolvedException().getClass()).isEqualTo(XYZ.class);
    

    It seemed to work.

提交回复
热议问题