diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcAutoConfiguration.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcAutoConfiguration.java index 9fc6e2a6a4b..f886e5b04ad 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcAutoConfiguration.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcAutoConfiguration.java @@ -16,7 +16,8 @@ package org.springframework.boot.test.autoconfigure.web.servlet; -import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; + import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; @@ -35,6 +36,7 @@ import org.springframework.web.context.WebApplicationContext; * Auto-configuration for {@link MockMvc}. * * @author Phillip Webb + * @author Andy Wilkinson * @see ImportWebMvcAutoConfiguration */ @Configuration @@ -43,20 +45,27 @@ import org.springframework.web.context.WebApplicationContext; @EnableConfigurationProperties class MockMvcAutoConfiguration { - @Autowired - private WebApplicationContext context; + private final WebApplicationContext context; + + MockMvcAutoConfiguration(WebApplicationContext context) { + this.context = context; + } @Bean @ConditionalOnMissingBean(MockMvcBuilder.class) - public DefaultMockMvcBuilder mockMvcBuilder() { - return MockMvcBuilders.webAppContextSetup(this.context) - .apply(mockMvcConfigurer()); + public DefaultMockMvcBuilder mockMvcBuilder( + List customizers) { + DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.context); + for (MockMvcBuilderCustomizer customizer : customizers) { + customizer.customize(builder); + } + return builder; } @Bean @ConfigurationProperties("spring.test.mockmvc") - public SpringBootMockMvcConfigurer mockMvcConfigurer() { - return new SpringBootMockMvcConfigurer(this.context); + public SpringBootMockMvcBuilderCustomizer springBootMockMvcBuilderCustomizer() { + return new SpringBootMockMvcBuilderCustomizer(this.context); } @Bean diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcBuilderCustomizer.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcBuilderCustomizer.java new file mode 100644 index 00000000000..ecf62f8b742 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcBuilderCustomizer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.web.servlet; + +import org.springframework.test.web.servlet.MockMvcBuilder; +import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; + +/** + * A customizer for a {@link ConfigurableMockMvcBuilder}. Any + * {@code MockMvcBuilderCustomizer} beans found in the application context will be + * {@link #customize called} to customize the auto-configured {@link MockMvcBuilder}. + * + * @author Andy Wilkinson + * @since 1.4.0 + * @see MockMvcAutoConfiguration + */ +public interface MockMvcBuilderCustomizer { + + /** + * Customize the given {@code builder}. + * @param builder the builder + */ + void customize(ConfigurableMockMvcBuilder builder); + +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcConfigurer.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java similarity index 83% rename from spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcConfigurer.java rename to spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java index 6797532d429..ba7b07b919e 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcConfigurer.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvcBuilderCustomizer.java @@ -24,22 +24,21 @@ import org.springframework.boot.context.embedded.DelegatingFilterProxyRegistrati import org.springframework.boot.context.embedded.FilterRegistrationBean; import org.springframework.boot.context.embedded.ServletContextInitializer; import org.springframework.boot.context.embedded.ServletContextInitializerBeans; -import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; -import org.springframework.test.web.servlet.setup.MockMvcConfigurer; import org.springframework.util.Assert; import org.springframework.web.context.WebApplicationContext; /** - * {@link MockMvcConfigurer} for a typical Spring Boot application. Usually applied + * {@link MockMvcBuilderCustomizer} for a typical Spring Boot application. Usually applied * automatically via {@link AutoConfigureMockMvc @AutoConfigureMockMvc}, but may also be * used directly. * * @author Phillip Webb + * @author Andy Wilkinson * @since 1.4.0 */ -public class SpringBootMockMvcConfigurer implements MockMvcConfigurer { +public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomizer { private final WebApplicationContext context; @@ -48,16 +47,16 @@ public class SpringBootMockMvcConfigurer implements MockMvcConfigurer { private boolean alwaysPrint = true; /** - * Create a new {@link SpringBootMockMvcConfigurer} instance. + * Create a new {@link SpringBootMockMvcBuilderCustomizer} instance. * @param context the source application context */ - public SpringBootMockMvcConfigurer(WebApplicationContext context) { + public SpringBootMockMvcBuilderCustomizer(WebApplicationContext context) { Assert.notNull(context, "Context must not be null"); this.context = context; } @Override - public void afterConfigurerAdded(ConfigurableMockMvcBuilder builder) { + public void customize(ConfigurableMockMvcBuilder builder) { if (this.addFilters) { addFilters(builder); } @@ -66,12 +65,6 @@ public class SpringBootMockMvcConfigurer implements MockMvcConfigurer { } } - @Override - public RequestPostProcessor beforeMockMvcCreated( - ConfigurableMockMvcBuilder builder, WebApplicationContext context) { - return null; - } - private void addFilters(ConfigurableMockMvcBuilder builder) { ServletContextInitializerBeans Initializers = new ServletContextInitializerBeans( this.context);