1

This may be a repeated question. But I have gone through some 10-15 related posts and associated responses which have not resolved my issue. The issue that I am facing is as here below I have a SpringRest controller class with custom ApplicationException. I have written a Junit for my controller and here below is the snippet where I am facing issue.

this.mockMvc.perform(MockMvcRequestBuilders.post(url)
        .contentType(MediaType.APPLICATION_JSON)
        .content(new ObjctMapper().writeValueAsString(requestObject)
        .headers(header)
        .accept(MediaType.APPLICATION_JSON)
    )
    .andDo(print())
    .andExpect(status().is4xxClientError());

When I execute the test method, I see that ApplicationException is thrown from the code, but the Junit fails and what I see in Junit Console is

"org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.abc.pmr.case.exception.ApplicationException at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
...
...
Caused by: com.abc.pmr.case.exception.ApplicationException
    at com.abc.pmr.case.exception.ApplicationException

I tried with .andExpect(mvcresult -> assertTrue(mvcresult.getResolvedException() instanceof ApplicationException)); as well. But that did not help either.

The Junit fails with the above said exception, while I want to pass the Junit with the expected Exception as ApplicationException. Note: All the success scenario testcases in this class are passing successfully.

Any help here would be much appreciated.

Ram
  • 327
  • 1
  • 4
  • 18
  • Do you have any handler logic for your exception? You've tried `getResolvedException`, but it will only `Return any exception raised by a handler and successfully resolved through a HandlerExceptionResolver.` from [docs](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/MvcResult.html#getResolvedException--) – maveriq Oct 11 '20 at 15:09
  • Yes, I do have an exception handler which further calls a transformer class that builds the complete error response along with error code, error messages, etc. – Ram Oct 11 '20 at 15:37
  • Could you add your exception handler listing here? – maveriq Oct 11 '20 at 15:39
  • Are you sure, the exception thrown by your controller is in 400 range? Can please add the response of Api for which it throws exception? – wak786 Oct 11 '20 at 16:00
  • @evg_nyWill share the handler code shortly. – Ram Oct 11 '20 at 16:02
  • @wak786, If the exception is not in 400 range, it should give assertion error. But the control does not even come to the line next to mockmvc.perform – Ram Oct 11 '20 at 16:04
  • @evg_ny here is the method of the exception handler ```@ExceptionHandler(ApplicationException.class) public ResponseEntity handleAccountDetailsException(ApplicationException ex) { return errorResponseTransformer.populateDefaultErrorResponse(ex); }``` The transformer code is as here below ```return buildErrorResponse(ErrorEnum.BAD_REQUEST_ERROR, "" );``` – Ram Oct 11 '20 at 16:17

1 Answers1

1

I suppose that your mockMvc setup is not completely correct for your case. Make sure that you have set exception handler to your mockMvc. You can find more info about this here.

So, when your environment will be configured correct you will have an ability test this logic.

maveriq
  • 456
  • 4
  • 14
  • Great, let me know then how its going. – maveriq Oct 11 '20 at 16:25
  • 1
    Awesome! You were on the spot! Only issue is I see different ways to set exception handler in MockMVC. Earlier I had written one which did not work. The working one is here below ```this.mockMvc = MockMvcBuilders.standaloneSetup(autowiredController).setControllerAdvice(autowiredExceptionHandler).build();``` Also on top of the class, I had to include the following ```@ContextConfiguration(classes = {MyController.class, Services.class, ApplicationException.class, ExceptionHandler.class, ErrorTransformer.class})``` Thanks again! – Ram Oct 11 '20 at 16:54
  • 1
    This link gave me the exact snippet. https://stackoverflow.com/questions/15302243/spring-mvc-controllers-unit-test-not-calling-controlleradvice/28727831#28727831 – Ram Oct 11 '20 at 16:54
  • Cool, so, it was pretty hard to give your concrete complete solution here because I don't know your full environment and spring setup. But anyway if it helped you - please mark this as accepted answer. – maveriq Oct 11 '20 at 16:57
  • 1
    I agree. I should have given you the @BeforeEach method contents, as that would have given you better idea. But could not share much of details as much of it are proprietary. So in the process of cutting down those pieces, I could have missed the required ones as well. Whatever... All good now. Accepted! Thanks! – Ram Oct 11 '20 at 17:08
  • Great! Nice to hear it :) – maveriq Oct 11 '20 at 17:09