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 and Type | Class and Description |
---|---|
private class |
StandaloneMockMvcBuilder.StandaloneConfiguration
Using the MVC Java configuration as the starting point for the "standalone" setup
|
private static class |
StandaloneMockMvcBuilder.StaticRequestMappingHandlerMapping
A
RequestMappingHandlerMapping that allows registration of controllers. |
private static class |
StandaloneMockMvcBuilder.StaticStringValueResolver
A static resolver placeholder for values embedded in request mappings.
|
private static class |
StandaloneMockMvcBuilder.StaticViewResolver
A
ViewResolver that always returns same View. |
Modifier | Constructor and Description |
---|---|
protected |
StandaloneMockMvcBuilder(java.lang.Object... controllers)
Protected constructor.
|
Modifier and Type | Method and Description |
---|---|
StandaloneMockMvcBuilder |
addInterceptors(HandlerInterceptor... interceptors)
Add interceptors mapped to all incoming requests.
|
StandaloneMockMvcBuilder |
addMappedInterceptors(java.lang.String[] pathPatterns,
HandlerInterceptor... interceptors)
Add interceptors mapped to a set of path patterns.
|
StandaloneMockMvcBuilder |
addPlaceholderValue(java.lang.String name,
java.lang.String value)
In a standalone setup there is no support for placeholder values embedded in
request mappings.
|
StandaloneMockMvcBuilder |
addPlaceHolderValue(java.lang.String name,
java.lang.String value)
Deprecated.
as of 4.2.8, in favor of
addPlaceholderValue(String, String) |
private java.util.List<ViewResolver> |
initViewResolvers(WebApplicationContext wac) |
protected WebApplicationContext |
initWebAppContext()
A method to obtain the WebApplicationContext to be passed to the DispatcherServlet.
|
private void |
registerMvcSingletons(StubWebApplicationContext wac) |
StandaloneMockMvcBuilder |
setAsyncRequestTimeout(long timeout)
Specify the timeout value for async execution.
|
StandaloneMockMvcBuilder |
setContentNegotiationManager(ContentNegotiationManager manager)
Set a ContentNegotiationManager.
|
StandaloneMockMvcBuilder |
setControllerAdvice(java.lang.Object... controllerAdvice)
Register one or more
ControllerAdvice instances to be used in tests. |
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 |
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(java.util.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
createMockMvc, createMockMvc
private final java.lang.Object[] controllers
private java.util.List<java.lang.Object> controllerAdvice
private java.util.List<HttpMessageConverter<?>> messageConverters
private java.util.List<HandlerMethodArgumentResolver> customArgumentResolvers
private java.util.List<HandlerMethodReturnValueHandler> customReturnValueHandlers
private final java.util.List<MappedInterceptor> mappedInterceptors
private Validator validator
private ContentNegotiationManager contentNegotiationManager
private FormattingConversionService conversionService
private java.util.List<HandlerExceptionResolver> handlerExceptionResolvers
private java.lang.Long asyncRequestTimeout
private java.util.List<ViewResolver> viewResolvers
private LocaleResolver localeResolver
private FlashMapManager flashMapManager
private boolean useSuffixPatternMatch
private boolean useTrailingSlashPatternMatch
private java.lang.Boolean removeSemicolonContent
private java.util.Map<java.lang.String,java.lang.String> placeholderValues
protected StandaloneMockMvcBuilder(java.lang.Object... controllers)
public StandaloneMockMvcBuilder setControllerAdvice(java.lang.Object... controllerAdvice)
ControllerAdvice
instances to be used in tests.
Normally @ControllerAdvice
are auto-detected as long as they're
declared as Spring beans. However since the standalone setup does not load
any Spring configuration 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(java.lang.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(java.util.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(java.lang.String name, java.lang.String value)
WebApplicationContext
.@Deprecated public StandaloneMockMvcBuilder addPlaceHolderValue(java.lang.String name, java.lang.String value)
addPlaceholderValue(String, String)
protected WebApplicationContext initWebAppContext()
AbstractMockMvcBuilder
AbstractMockMvcBuilder.build()
before the
MockMvc
instance is created.initWebAppContext
in class AbstractMockMvcBuilder<StandaloneMockMvcBuilder>
private void registerMvcSingletons(StubWebApplicationContext wac)
private java.util.List<ViewResolver> initViewResolvers(WebApplicationContext wac)