public MockMvc mvc(Object controller) { StandaloneMockMvcBuilder builder = new StandaloneMockMvcBuilder(controller) { @Override protected WebApplicationContext initWebAppContext() { WebApplicationContext context = super.initWebAppContext(); StaticListableBeanFactory beanFactory = (StaticListableBeanFactory)context.getAutowireCapableBeanFactory(); Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) .filter(name -> applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) .forEach(name -> beanFactory.addBean(name, applicationContext.getBean(name))); context.getBean(RequestMappingHandlerAdapter.class).afterPropertiesSet(); return context; } }; return builder.setHandlerExceptionResolvers(handlerExceptionResolver).build(); }
protected void configure(final StandaloneMockMvcBuilder mockMvcBuilder) { mockMvcBuilder.setCustomArgumentResolvers(mockMvcCustomArgumentResolvers()); mockMvcBuilder.setMessageConverters(mockMvcMessageConverters()); final DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); mockMvcBuilder.setConversionService(conversionService); }
@Override protected void configure(final StandaloneMockMvcBuilder mockMvcBuilder) { super.configure(mockMvcBuilder); mockMvcBuilder.alwaysDo(print()); }
@Before public void setUpMockMvc() throws Exception { final StandaloneMockMvcBuilder mockMvcBuilder = MockMvcBuilders.standaloneSetup(mockMvcControllers()); configure(mockMvcBuilder); mockMvc = mockMvcBuilder.build(); }