Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
b630b080
Commit
b630b080
authored
Apr 04, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make it easier to customize the auto-configured MockMvcBuilder
Closes gh-5556
parent
611d4417
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
21 deletions
+62
-21
MockMvcAutoConfiguration.java
...t/autoconfigure/web/servlet/MockMvcAutoConfiguration.java
+17
-8
MockMvcBuilderCustomizer.java
...t/autoconfigure/web/servlet/MockMvcBuilderCustomizer.java
+39
-0
SpringBootMockMvcBuilderCustomizer.java
...igure/web/servlet/SpringBootMockMvcBuilderCustomizer.java
+6
-13
No files found.
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcAutoConfiguration.java
View file @
b630b080
...
@@ -16,7 +16,8 @@
...
@@ -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;
...
@@ -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;
...
@@ -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
SpringBootMockMvc
Configurer
mockMvcConfigur
er
()
{
public
SpringBootMockMvc
BuilderCustomizer
springBootMockMvcBuilderCustomiz
er
()
{
return
new
SpringBootMockMvc
Configur
er
(
this
.
context
);
return
new
SpringBootMockMvc
BuilderCustomiz
er
(
this
.
context
);
}
}
@Bean
@Bean
...
...
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/MockMvcBuilderCustomizer.java
0 → 100644
View file @
b630b080
/*
* 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
);
}
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvc
Configur
er.java
→
spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/SpringBootMockMvc
BuilderCustomiz
er.java
View file @
b630b080
...
@@ -24,22 +24,21 @@ import org.springframework.boot.context.embedded.DelegatingFilterProxyRegistrati
...
@@ -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 MockMvc
Configur
er} for a typical Spring Boot application. Usually applied
* {@link MockMvc
BuilderCustomiz
er} 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
SpringBootMockMvc
Configurer
implements
MockMvcConfigur
er
{
public
class
SpringBootMockMvc
BuilderCustomizer
implements
MockMvcBuilderCustomiz
er
{
private
final
WebApplicationContext
context
;
private
final
WebApplicationContext
context
;
...
@@ -48,16 +47,16 @@ public class SpringBootMockMvcConfigurer implements MockMvcConfigurer {
...
@@ -48,16 +47,16 @@ public class SpringBootMockMvcConfigurer implements MockMvcConfigurer {
private
boolean
alwaysPrint
=
true
;
private
boolean
alwaysPrint
=
true
;
/**
/**
* Create a new {@link SpringBootMockMvc
Configur
er} instance.
* Create a new {@link SpringBootMockMvc
BuilderCustomiz
er} instance.
* @param context the source application context
* @param context the source application context
*/
*/
public
SpringBootMockMvc
Configur
er
(
WebApplicationContext
context
)
{
public
SpringBootMockMvc
BuilderCustomiz
er
(
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 {
...
@@ -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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment