public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneMockMvcBuilder>
MockMvcBuilder
that accepts @Controller
registrations
thus allowing full control over the instantiation and initialization of
controllers and their dependencies similar to plain unit tests, and also
making it possible to test one controller at a time.
This builder creates the minimum infrastructure required by the
DispatcherServlet
to serve requests with annotated controllers and
also provides methods for customization. The resulting configuration and
customization options are equivalent to using MVC Java config except
using builder style methods.
To configure view resolution, either select a "fixed" view to use for every
request performed (see setSingleView(View)
) or provide a list of
ViewResolver
s (see setViewResolvers(ViewResolver...)
).
Modifier | Constructor and Description |
---|---|
protected |
StandaloneMockMvcBuilder(Object... controllers)
Protected constructor.
|
Modifier and Type | Method and Description |
---|---|
StandaloneMockMvcBuilder |
addInterceptors(HandlerInterceptor... interceptors)
Add interceptors mapped to all incoming requests.
|
StandaloneMockMvcBuilder |
addMappedInterceptors(String[] pathPatterns,
HandlerInterceptor... interceptors)
Add interceptors mapped to a set of path patterns.
|
StandaloneMockMvcBuilder |
addPlaceholderValue(String name,
String value)
In a standalone setup there is no support for placeholder values embedded in
request mappings.
|
protected WebApplicationContext |
initWebAppContext()
A method to obtain the
WebApplicationContext to be passed to the
DispatcherServlet . |
StandaloneMockMvcBuilder |
setAsyncRequestTimeout(long timeout)
Specify the timeout value for async execution.
|
StandaloneMockMvcBuilder |
setContentNegotiationManager(ContentNegotiationManager manager)
Set a ContentNegotiationManager.
|
StandaloneMockMvcBuilder |
setControllerAdvice(Object... controllerAdvice)
Register one or more
ControllerAdvice
instances to be used in tests (specified Class will be turned into instance). |
StandaloneMockMvcBuilder |
setConversionService(FormattingConversionService conversionService)
Provide a conversion service with custom formatters and converters.
|
StandaloneMockMvcBuilder |
setCustomArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers)
Provide custom resolvers for controller method arguments.
|
StandaloneMockMvcBuilder |
setCustomHandlerMapping(Supplier<RequestMappingHandlerMapping> factory)
Configure factory to create a custom
RequestMappingHandlerMapping . |
StandaloneMockMvcBuilder |
setCustomReturnValueHandlers(HandlerMethodReturnValueHandler... handlers)
Provide custom handlers for controller method return values.
|
StandaloneMockMvcBuilder |
setFlashMapManager(FlashMapManager flashMapManager)
Provide a custom FlashMapManager instance.
|
StandaloneMockMvcBuilder |
setHandlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers)
Set the HandlerExceptionResolver types to use as an array.
|
StandaloneMockMvcBuilder |
setHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers)
Set the HandlerExceptionResolver types to use as a list.
|
StandaloneMockMvcBuilder |
setLocaleResolver(LocaleResolver localeResolver)
Provide a LocaleResolver instance.
|
StandaloneMockMvcBuilder |
setMessageConverters(HttpMessageConverter<?>... messageConverters)
Set the message converters to use in argument resolvers and in return value
handlers, which support reading and/or writing to the body of the request
and response.
|
StandaloneMockMvcBuilder |
setRemoveSemicolonContent(boolean removeSemicolonContent)
Set if ";" (semicolon) content should be stripped from the request URI.
|
StandaloneMockMvcBuilder |
setSingleView(View view)
Sets up a single
ViewResolver that always returns the provided
view instance. |
StandaloneMockMvcBuilder |
setUseSuffixPatternMatch(boolean useSuffixPatternMatch)
Whether to use suffix pattern match (".*") when matching patterns to
requests.
|
StandaloneMockMvcBuilder |
setUseTrailingSlashPatternMatch(boolean useTrailingSlashPatternMatch)
Whether to match to URLs irrespective of the presence of a trailing slash.
|
StandaloneMockMvcBuilder |
setValidator(Validator validator)
Provide a custom
Validator instead of the one created by default. |
StandaloneMockMvcBuilder |
setViewResolvers(ViewResolver... resolvers)
Set up view resolution with the given
ViewResolver s. |
addDispatcherServletCustomizer, addFilter, addFilters, alwaysDo, alwaysExpect, apply, build, defaultRequest, dispatchOptions, self
createMockMvc
protected StandaloneMockMvcBuilder(Object... controllers)
public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice)
ControllerAdvice
instances to be used in tests (specified Class
will be turned into instance).
Normally @ControllerAdvice
are auto-detected as long as they're declared
as Spring beans. However since the standalone setup does not load any Spring config,
they need to be registered explicitly here instead much like controllers.
public StandaloneMockMvcBuilder setMessageConverters(HttpMessageConverter<?>... messageConverters)
public StandaloneMockMvcBuilder setValidator(Validator validator)
Validator
instead of the one created by default.
The default implementation used, assuming JSR-303 is on the classpath, is
LocalValidatorFactoryBean
.public StandaloneMockMvcBuilder setConversionService(FormattingConversionService conversionService)
DefaultFormattingConversionService
is used by default.public StandaloneMockMvcBuilder addInterceptors(HandlerInterceptor... interceptors)
public StandaloneMockMvcBuilder addMappedInterceptors(@Nullable String[] pathPatterns, HandlerInterceptor... interceptors)
public StandaloneMockMvcBuilder setContentNegotiationManager(ContentNegotiationManager manager)
public StandaloneMockMvcBuilder setAsyncRequestTimeout(long timeout)
timeout
- the timeout value in millisecondspublic StandaloneMockMvcBuilder setCustomArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers)
public StandaloneMockMvcBuilder setCustomReturnValueHandlers(HandlerMethodReturnValueHandler... handlers)
public StandaloneMockMvcBuilder setHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers)
public StandaloneMockMvcBuilder setHandlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers)
public StandaloneMockMvcBuilder setViewResolvers(ViewResolver... resolvers)
ViewResolver
s.
If not set, an InternalResourceViewResolver
is used by default.public StandaloneMockMvcBuilder setSingleView(View view)
ViewResolver
that always returns the provided
view instance. This is a convenient shortcut if you need to use one
View instance only -- e.g. rendering generated content (JSON, XML, Atom).public StandaloneMockMvcBuilder setLocaleResolver(LocaleResolver localeResolver)
AcceptHeaderLocaleResolver
.public StandaloneMockMvcBuilder setFlashMapManager(FlashMapManager flashMapManager)
SessionFlashMapManager
is used by default.public StandaloneMockMvcBuilder setUseSuffixPatternMatch(boolean useSuffixPatternMatch)
The default value is true
.
public StandaloneMockMvcBuilder setUseTrailingSlashPatternMatch(boolean useTrailingSlashPatternMatch)
The default value is true
.
public StandaloneMockMvcBuilder setRemoveSemicolonContent(boolean removeSemicolonContent)
AbstractHandlerMapping.setRemoveSemicolonContent(boolean)
.public StandaloneMockMvcBuilder addPlaceholderValue(String name, String value)
WebApplicationContext
.public StandaloneMockMvcBuilder setCustomHandlerMapping(Supplier<RequestMappingHandlerMapping> factory)
RequestMappingHandlerMapping
.factory
- the factoryprotected WebApplicationContext initWebAppContext()
AbstractMockMvcBuilder
WebApplicationContext
to be passed to the
DispatcherServlet
. Invoked from AbstractMockMvcBuilder.build()
before the
MockMvc
instance is created.initWebAppContext
in class AbstractMockMvcBuilder<StandaloneMockMvcBuilder>