Class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
java.lang.Object
org.springframework.test.web.servlet.MockMvcBuilderSupport
org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder<B>
- Type Parameters:
B
- a self reference to the builder type
- All Implemented Interfaces:
MockMvcBuilder
,ConfigurableMockMvcBuilder<B>
- Direct Known Subclasses:
DefaultMockMvcBuilder
,StandaloneMockMvcBuilder
public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
extends MockMvcBuilderSupport
implements ConfigurableMockMvcBuilder<B>
Abstract implementation of
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
.
- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Stephane Nicoll, Sam Brannen
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends B>
TA more advanced variant ofConfigurableMockMvcBuilder.dispatchOptions(boolean)
that allows customizing anyDispatcherServlet
property.final <T extends B>
TAdd a filter mapped to a specific set of patterns.final <T extends B>
TaddFilters
(Filter... filters) Add filters mapped to any request (i.e.final <T extends B>
TalwaysDo
(ResultHandler resultHandler) Define a global action that should always be applied to every response.final <T extends B>
TalwaysExpect
(ResultMatcher resultMatcher) Define a global expectation that should always be applied to every response.final <T extends B>
Tapply
(MockMvcConfigurer configurer) Add aMockMvcConfigurer
that automates MockMvc setup and configures it for some specific purpose (e.g.final MockMvc
build()
Build aMockMvc
instance.final <T extends B>
TdefaultRequest
(RequestBuilder requestBuilder) Define default request properties that should be merged into all performed requests.final <T extends B>
TdefaultResponseCharacterEncoding
(Charset defaultResponseCharacterEncoding) Define the default character encoding to be applied to every response.final <T extends B>
TdispatchOptions
(boolean dispatchOptions) Whether to enable the DispatcherServlet propertydispatchOptionsRequest
which allows processing of HTTP OPTIONS requests.protected abstract WebApplicationContext
A method to obtain theWebApplicationContext
to be passed to theDispatcherServlet
.protected <T extends B>
Tself()
Methods inherited from class org.springframework.test.web.servlet.MockMvcBuilderSupport
createMockMvc, createMockMvc
-
Constructor Details
-
AbstractMockMvcBuilder
public AbstractMockMvcBuilder()
-
-
Method Details
-
addFilters
Description copied from interface:ConfigurableMockMvcBuilder
Add filters mapped to any request (i.e. "/*"). For example:mockMvcBuilder.addFilters(springSecurityFilterChain);
It 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.
- Specified by:
addFilters
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
filters
- the filters to add
-
addFilter
Description copied from interface:ConfigurableMockMvcBuilder
Add a filter mapped to a specific set of patterns. For example:mockMvcBuilder.addFilter(myResourceFilter, "/resources/*");
It 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.
- Specified by:
addFilter
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
filter
- the filter to addurlPatterns
- the URL patterns to map to; if empty, "/*" is used by default
-
defaultRequest
Description copied from interface:ConfigurableMockMvcBuilder
Define default request properties that should be merged into all performed requests. In effect this provides a mechanism for defining common initialization for all requests such as the content type, request parameters, session attributes, and any other request property.Properties specified at the time of performing a request override the default properties defined here.
- Specified by:
defaultRequest
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
requestBuilder
- a RequestBuilder; see static factory methods inMockMvcRequestBuilders
-
defaultResponseCharacterEncoding
public final <T extends B> T defaultResponseCharacterEncoding(Charset defaultResponseCharacterEncoding) Define the default character encoding to be applied to every response.- Specified by:
defaultResponseCharacterEncoding
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
defaultResponseCharacterEncoding
- the default response character encoding- Since:
- 5.3.10
-
alwaysExpect
Description copied from interface:ConfigurableMockMvcBuilder
Define a global expectation that should always be applied to every response. For example, status code 200 (OK), content type"application/json"
, etc.- Specified by:
alwaysExpect
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
resultMatcher
- a ResultMatcher; see static factory methods inMockMvcResultMatchers
-
alwaysDo
Description copied from interface:ConfigurableMockMvcBuilder
Define a global action that should always be applied to every response. For example, writing detailed information about the performed request and resulting response toSystem.out
.- Specified by:
alwaysDo
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
resultHandler
- a ResultHandler; see static factory methods inMockMvcResultHandlers
-
addDispatcherServletCustomizer
Description copied from interface:ConfigurableMockMvcBuilder
A more advanced variant ofConfigurableMockMvcBuilder.dispatchOptions(boolean)
that allows customizing anyDispatcherServlet
property.- Specified by:
addDispatcherServletCustomizer
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
-
dispatchOptions
Description copied from interface:ConfigurableMockMvcBuilder
Whether to enable the DispatcherServlet propertydispatchOptionsRequest
which allows processing of HTTP OPTIONS requests.- Specified by:
dispatchOptions
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
-
apply
Description copied from interface:ConfigurableMockMvcBuilder
Add aMockMvcConfigurer
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.- Specified by:
apply
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- See Also:
-
self
-
build
Build aMockMvc
instance.- Specified by:
build
in interfaceMockMvcBuilder
-
initWebAppContext
-