Make it easier to customize the auto-configured MockMvcBuilder
Closes gh-5556
This commit is contained in:
parent
611d441763
commit
b630b080ce
|
|
@ -16,7 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.boot.test.autoconfigure.web.servlet;
|
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.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||||
|
|
@ -35,6 +36,7 @@ import org.springframework.web.context.WebApplicationContext;
|
||||||
* Auto-configuration for {@link MockMvc}.
|
* Auto-configuration for {@link MockMvc}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Andy Wilkinson
|
||||||
* @see ImportWebMvcAutoConfiguration
|
* @see ImportWebMvcAutoConfiguration
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|
@ -43,20 +45,27 @@ import org.springframework.web.context.WebApplicationContext;
|
||||||
@EnableConfigurationProperties
|
@EnableConfigurationProperties
|
||||||
class MockMvcAutoConfiguration {
|
class MockMvcAutoConfiguration {
|
||||||
|
|
||||||
@Autowired
|
private final WebApplicationContext context;
|
||||||
private WebApplicationContext context;
|
|
||||||
|
MockMvcAutoConfiguration(WebApplicationContext context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(MockMvcBuilder.class)
|
@ConditionalOnMissingBean(MockMvcBuilder.class)
|
||||||
public DefaultMockMvcBuilder mockMvcBuilder() {
|
public DefaultMockMvcBuilder mockMvcBuilder(
|
||||||
return MockMvcBuilders.webAppContextSetup(this.context)
|
List<MockMvcBuilderCustomizer> customizers) {
|
||||||
.apply(mockMvcConfigurer());
|
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.context);
|
||||||
|
for (MockMvcBuilderCustomizer customizer : customizers) {
|
||||||
|
customizer.customize(builder);
|
||||||
|
}
|
||||||
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConfigurationProperties("spring.test.mockmvc")
|
@ConfigurationProperties("spring.test.mockmvc")
|
||||||
public SpringBootMockMvcConfigurer mockMvcConfigurer() {
|
public SpringBootMockMvcBuilderCustomizer springBootMockMvcBuilderCustomizer() {
|
||||||
return new SpringBootMockMvcConfigurer(this.context);
|
return new SpringBootMockMvcBuilderCustomizer(this.context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -24,22 +24,21 @@ import org.springframework.boot.context.embedded.DelegatingFilterProxyRegistrati
|
||||||
import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
||||||
import org.springframework.boot.context.embedded.ServletContextInitializer;
|
import org.springframework.boot.context.embedded.ServletContextInitializer;
|
||||||
import org.springframework.boot.context.embedded.ServletContextInitializerBeans;
|
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.result.MockMvcResultHandlers;
|
||||||
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
|
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
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
|
* automatically via {@link AutoConfigureMockMvc @AutoConfigureMockMvc}, but may also be
|
||||||
* used directly.
|
* used directly.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Andy Wilkinson
|
||||||
* @since 1.4.0
|
* @since 1.4.0
|
||||||
*/
|
*/
|
||||||
public class SpringBootMockMvcConfigurer implements MockMvcConfigurer {
|
public class SpringBootMockMvcBuilderCustomizer implements MockMvcBuilderCustomizer {
|
||||||
|
|
||||||
private final WebApplicationContext context;
|
private final WebApplicationContext context;
|
||||||
|
|
||||||
|
|
@ -48,16 +47,16 @@ public class SpringBootMockMvcConfigurer implements MockMvcConfigurer {
|
||||||
private boolean alwaysPrint = true;
|
private boolean alwaysPrint = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@link SpringBootMockMvcConfigurer} instance.
|
* Create a new {@link SpringBootMockMvcBuilderCustomizer} instance.
|
||||||
* @param context the source application context
|
* @param context the source application context
|
||||||
*/
|
*/
|
||||||
public SpringBootMockMvcConfigurer(WebApplicationContext context) {
|
public SpringBootMockMvcBuilderCustomizer(WebApplicationContext context) {
|
||||||
Assert.notNull(context, "Context must not be null");
|
Assert.notNull(context, "Context must not be null");
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterConfigurerAdded(ConfigurableMockMvcBuilder<?> builder) {
|
public void customize(ConfigurableMockMvcBuilder<?> builder) {
|
||||||
if (this.addFilters) {
|
if (this.addFilters) {
|
||||||
addFilters(builder);
|
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) {
|
private void addFilters(ConfigurableMockMvcBuilder<?> builder) {
|
||||||
ServletContextInitializerBeans Initializers = new ServletContextInitializerBeans(
|
ServletContextInitializerBeans Initializers = new ServletContextInitializerBeans(
|
||||||
this.context);
|
this.context);
|
||||||
Loading…
Reference in New Issue