Make it easier to customize the auto-configured MockMvcBuilder

Closes gh-5556
This commit is contained in:
Andy Wilkinson 2016-04-04 13:35:52 +01:00
parent 611d441763
commit b630b080ce
3 changed files with 62 additions and 21 deletions

View File

@ -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<MockMvcBuilderCustomizer> 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

View File

@ -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);
}

View File

@ -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);