/** * Create a new instance using the supplied {@link WebApplicationContext} * and {@link MockMvcConfigurer}. * @param context the {@code WebApplicationContext} to create a {@code MockMvc} * instance from; never {@code null} * @param configurer the MockMvcConfigurer to apply; never {@code null} */ protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context, MockMvcConfigurer configurer) { this(MockMvcBuilders.webAppContextSetup(context).apply(configurer).build()); }
/** * Create a new instance using the supplied {@link WebApplicationContext}. * @param context the {@code WebApplicationContext} to create a {@code MockMvc} * instance from; never {@code null} */ protected MockMvcWebConnectionBuilderSupport(WebApplicationContext context) { this(MockMvcBuilders.webAppContextSetup(context).build()); }
/** * Add interceptors mapped to all incoming requests. */ public StandaloneMockMvcBuilder addInterceptors(HandlerInterceptor... interceptors) { addMappedInterceptors(null, interceptors); return this; }
@Test(expected = IllegalArgumentException.class) public void addFilterPatternsNull() { StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(new PersonController()); builder.addFilter(new ContinueFilter(), (String[]) null); }
@Test(expected = IllegalArgumentException.class) public void addFiltersFiltersContainsNull() { StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(new PersonController()); builder.addFilters(new ContinueFilter(), (Filter) null); }
@Test(expected = IllegalArgumentException.class) public void addFiltersFiltersNull() { StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(new PersonController()); builder.addFilters((Filter[]) null); }
public final <T extends B> T apply(MockMvcConfigurer configurer) { configurer.afterConfigurerAdded(this); this.configurers.add(configurer); return self(); }
@Override protected WebApplicationContext initWebAppContext() { MockServletContext servletContext = new MockServletContext(); StubWebApplicationContext wac = new StubWebApplicationContext(servletContext); registerMvcSingletons(wac); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); return wac; }
/** * Protected constructor. Not intended for direct instantiation. * @see MockMvcBuilders#standaloneSetup(Object...) */ protected StandaloneMockMvcBuilder(Object... controllers) { this.controllers = instantiateIfNecessary(controllers); }
@Override public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException { return this.beanFactory.getBean(requiredType, args); }
/** * Build a {@link MockMvc} instance using the given, fully initialized * (i.e., <em>refreshed</em>) {@link WebApplicationContext}. * <p>The {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} * will use the context to discover Spring MVC infrastructure and application * controllers in it. The context must have been configured with a * {@link javax.servlet.ServletContext ServletContext}. */ public static DefaultMockMvcBuilder webAppContextSetup(WebApplicationContext context) { return new DefaultMockMvcBuilder(context); }
@Override public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException { return this.beanFactory.getBeansWithAnnotation(annotationType); }
@Override public <T> ObjectProvider<T> getBeanProvider(Class<T> requiredType) { return this.beanFactory.getBeanProvider(requiredType); }
@Override public String[] getBeanDefinitionNames() { return this.beanFactory.getBeanDefinitionNames(); }
@Override public boolean containsLocalBean(String name) { return this.beanFactory.containsBean(name); }
@Override public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException { return this.beanFactory.isTypeMatch(name, typeToMatch); }
@Override protected WebApplicationContext initWebAppContext() { this.wac = super.initWebAppContext(); return this.wac; } }
@Test(expected = IllegalArgumentException.class) public void addFilterPatternContainsNull() { StandaloneMockMvcBuilder builder = MockMvcBuilders.standaloneSetup(new PersonController()); builder.addFilter(new ContinueFilter(), (String) null); }
/** * Register one or more {@link org.springframework.web.bind.annotation.ControllerAdvice} * instances to be used in tests (specified {@code Class} will be turned into instance). * <p>Normally {@code @ControllerAdvice} are auto-detected as long as they're declared * as Spring beans. However since the standalone setup does not load any Spring config, * they need to be registered explicitly here instead much like controllers. * @since 4.2 */ public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice) { this.controllerAdvice = instantiateIfNecessary(controllerAdvice); return this; }
@Override public Object getBean(String name, Object... args) throws BeansException { return this.beanFactory.getBean(name, args); }