public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>> extends MockMvcBuilderSupport implements ConfigurableMockMvcBuilder<B>
MockMvcBuilder
with common methods for
configuring filters, default request properties, global expectations and
global result actions.
Subclasses can use different strategies to prepare the Spring
WebApplicationContext
that will be passed to the
DispatcherServlet
.
Constructor and Description |
---|
AbstractMockMvcBuilder() |
Modifier and Type | Method and Description |
---|---|
<T extends B> |
addDispatcherServletCustomizer(DispatcherServletCustomizer customizer) |
<T extends B> |
addFilter(Filter filter,
java.lang.String... urlPatterns)
Add a filter mapped to a specific set of patterns.
|
<T extends B> |
addFilters(Filter... filters)
Add filters mapped to any request (i.e.
|
<T extends B> |
alwaysDo(ResultHandler resultHandler)
Define a global action that should always be applied to every
response.
|
<T extends B> |
alwaysExpect(ResultMatcher resultMatcher)
Define a global expectation that should always be applied to
every response.
|
<T extends B> |
apply(MockMvcConfigurer configurer)
Add a
MockMvcConfigurer that automates MockMvc setup and
configures it for some specific purpose (e.g. |
MockMvc |
build()
Build a
MockMvc instance. |
<T extends B> |
defaultRequest(RequestBuilder requestBuilder)
Define default request properties that should be merged into all
performed requests.
|
<T extends B> |
dispatchOptions(boolean dispatchOptions)
Whether to enable the DispatcherServlet property
dispatchOptionsRequest which allows processing of HTTP OPTIONS requests. |
protected abstract WebApplicationContext |
initWebAppContext()
A method to obtain the
WebApplicationContext to be passed to the
DispatcherServlet . |
protected <T extends B> |
self() |
createMockMvc
public final <T extends B> T addFilters(Filter... filters)
ConfigurableMockMvcBuilder
mockMvcBuilder.addFilters(springSecurityFilterChain);
is the equivalent of the following web.xml configuration:
<filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Filters will be invoked in the order in which they are provided.
addFilters
in interface ConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
filters
- the filters to addpublic final <T extends B> T addFilter(Filter filter, java.lang.String... urlPatterns)
ConfigurableMockMvcBuilder
mockMvcBuilder.addFilters(myResourceFilter, "/resources/*");
is the equivalent of:
<filter-mapping> <filter-name>myResourceFilter</filter-name> <url-pattern>/resources/*</url-pattern> </filter-mapping>
Filters will be invoked in the order in which they are provided.
addFilter
in interface ConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
filter
- the filter to addurlPatterns
- URL patterns to map to; if empty, "/*" is used by defaultpublic final <T extends B> T defaultRequest(RequestBuilder requestBuilder)
ConfigurableMockMvcBuilder
Properties specified at the time of performing a request override the default properties defined here.
defaultRequest
in interface ConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
requestBuilder
- a RequestBuilder; see static factory methods in
MockMvcRequestBuilders
public final <T extends B> T alwaysExpect(ResultMatcher resultMatcher)
ConfigurableMockMvcBuilder
"application/json"
, etc.alwaysExpect
in interface ConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
resultMatcher
- a ResultMatcher; see static factory methods in
MockMvcResultMatchers
public final <T extends B> T alwaysDo(ResultHandler resultHandler)
ConfigurableMockMvcBuilder
System.out
.alwaysDo
in interface ConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
resultHandler
- a ResultHandler; see static factory methods in
MockMvcResultHandlers
public final <T extends B> T addDispatcherServletCustomizer(DispatcherServletCustomizer customizer)
public final <T extends B> T dispatchOptions(boolean dispatchOptions)
ConfigurableMockMvcBuilder
dispatchOptionsRequest
which allows processing of HTTP OPTIONS requests.dispatchOptions
in interface ConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
public final <T extends B> T apply(MockMvcConfigurer configurer)
ConfigurableMockMvcBuilder
MockMvcConfigurer
that automates MockMvc setup and
configures it for some specific purpose (e.g. security).
There is a built-in SharedHttpSessionConfigurer
that can be
used to re-use the HTTP session across requests. 3rd party frameworks
like Spring Security also use this mechanism to provide configuration
shortcuts.
apply
in interface ConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
SharedHttpSessionConfigurer
protected <T extends B> T self()
public final MockMvc build()
MockMvc
instance.build
in interface MockMvcBuilder
protected abstract WebApplicationContext initWebAppContext()