Register @ControllerAdvice annotated Controller in JUnitTest with MockMVC

后端 未结 5 2064
孤街浪徒
孤街浪徒 2021-01-01 10:10

My @ControllerAdvice annotated Controller looks like this:

@ControllerAdvice
public class GlobalControllerExceptionHandler {

    @ResponseStatu         


        
5条回答
  •  情深已故
    2021-01-01 11:03

    Got past the NestedServletException with the following solution...

        final StaticApplicationContext applicationContext = new StaticApplicationContext();
        applicationContext.registerSingleton("exceptionHandler", GlobalControllerExceptionHandler.class);
    
        final WebMvcConfigurationSupport webMvcConfigurationSupport = new WebMvcConfigurationSupport();
        webMvcConfigurationSupport.setApplicationContext(applicationContext);
    
        mockMvc = MockMvcBuilders.standaloneSetup(controller).
            setHandlerExceptionResolvers(webMvcConfigurationSupport.handlerExceptionResolver()).
            build();
    

提交回复
热议问题