Class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- java.lang.Object
- org.springframework.test.web.servlet.MockMvcBuilderSupport
- org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder<B>
- All Implemented Interfaces:
MockMvcBuilder
,ConfigurableMockMvcBuilder<B>
- Direct Known Subclasses:
DefaultMockMvcBuilder
,StandaloneMockMvcBuilder
public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>> extends MockMvcBuilderSupport implements ConfigurableMockMvcBuilder<B>
An abstract implementation ofMockMvcBuilder
with common methods for configuring filters, default request properties, global expectations and global result actions.Sub-classes can use different strategies to prepare a WebApplicationContext to pass to the DispatcherServlet.
- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Stephane Nicoll
Constructor Summary
Constructors Constructor Description AbstractMockMvcBuilder()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <T extends B>
TaddDispatcherServletCustomizer(DispatcherServletCustomizer customizer)
<T extends B>
TaddFilter(Filter filter, String... urlPatterns)
Add a filter mapped to a specific set of patterns.<T extends B>
TaddFilters(Filter... filters)
Add filters mapped to any request (i.e.<T extends B>
TalwaysDo(ResultHandler resultHandler)
Define a global action that should always be applied to every response.<T extends B>
TalwaysExpect(ResultMatcher resultMatcher)
Define a global expectation that should always be applied to every response.<T extends B>
Tapply(MockMvcConfigurer configurer)
Add aMockMvcConfigurer
that automates MockMvc setup and configures it for some specific purpose (e.g.MockMvc
build()
Build aMockMvc
instance.<T extends B>
TdefaultRequest(RequestBuilder requestBuilder)
Define default request properties that should be merged into all performed requests.<T extends B>
TdispatchOptions(boolean dispatchOptions)
Whether to enable the DispatcherServlet propertydispatchOptionsRequest
which allows processing of HTTP OPTIONS requests.protected abstract WebApplicationContext
initWebAppContext()
A method to obtain the WebApplicationContext to be passed to the DispatcherServlet.Methods inherited from class org.springframework.test.web.servlet.MockMvcBuilderSupport
createMockMvc, createMockMvc
Constructor Detail
AbstractMockMvcBuilder
public AbstractMockMvcBuilder()
Method Detail
addFilters
public final <T extends B> T addFilters(Filter... filters)
Description copied from interface:ConfigurableMockMvcBuilder
Add filters mapped to any request (i.e. "/*"). For example: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.
- Specified by:
addFilters
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
filters
- the filters to add
addFilter
public final <T extends B> T addFilter(Filter filter, String... urlPatterns)
Description copied from interface:ConfigurableMockMvcBuilder
Add a filter mapped to a specific set of patterns. For example: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.
- Specified by:
addFilter
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
- Parameters:
filter
- the filter to addurlPatterns
- URL patterns to map to; if empty, "/*" is used by default
defaultRequest
public final <T extends B> T defaultRequest(RequestBuilder requestBuilder)
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
.
alwaysExpect
public final <T extends B> T alwaysExpect(ResultMatcher resultMatcher)
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
public final <T extends B> T alwaysDo(ResultHandler resultHandler)
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
public final <T extends B> T addDispatcherServletCustomizer(DispatcherServletCustomizer customizer)
dispatchOptions
public final <T extends B> T dispatchOptions(boolean 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
public final <T extends B> T apply(MockMvcConfigurer configurer)
Description copied from interface:ConfigurableMockMvcBuilder
Add aMockMvcConfigurer
that automates MockMvc setup and configures it for some specific purpose (e.g. security).- Specified by:
apply
in interfaceConfigurableMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
build
public final MockMvc build()
Build aMockMvc
instance.- Specified by:
build
in interfaceMockMvcBuilder
initWebAppContext
protected abstract WebApplicationContext initWebAppContext()