How to prevent NestedServletException when testing Spring endpoints?

前端 未结 3 1303
耶瑟儿~
耶瑟儿~ 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:38

    I did spring security test cases by following this link. Things worked fine except this issue of nesting original exception in NestedServletException. I did not find any direct way to figure this out but AspectJ helped me in handling this in a cleaner way.

    We can use the static assertThatThrownBy() method of the Assertions class. This method returns an AbstractThrowableAssert object that we can use to write assertions for the thrown exception.

    The code that captures an exception thrown by the methodThatThrowsException() method looks as follows:

    assertThatThrownBy(() -> methodThatThrowsException())
    .isExactlyInstanceOf(DuplicateEmailException.class);
    

    Thanks to this excellent blog where you can find additional details.

    The way in which I handled this in my test case would be (by taking your test case codeline):

    org.assertj.core.api.Assertions.assertThatThrownBy(() -> mvc.perform(get("/api/scope")).andExpect(status().isOk())).hasCause(new AccessDeniedException("Access is denied"));
    

    That way your test case would be able to assert actual AccessDeniedException that is nested in NestedServletException.

提交回复
热议问题