How to prevent NestedServletException when testing Spring endpoints? How to prevent NestedServletException when testing Spring endpoints? spring spring

How to prevent NestedServletException when testing Spring endpoints?


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.


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:

// thenMvcResult 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.


I fixed it by adding an @ExceptionHandler for that exception. Seems like if MockMvc throws an actual exception it means you don't "handle" this case which is not ideal.