Class StandaloneMockMvcBuilder
- All Implemented Interfaces:
- MockMvcBuilder,- ConfigurableMockMvcBuilder<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
 ViewResolvers (see setViewResolvers(ViewResolver...)).
- Since:
- 3.2
- Author:
- Rossen Stoyanchev
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedStandaloneMockMvcBuilder(Object... controllers) Protected constructor.
- 
Method SummaryModifier and TypeMethodDescriptionaddInterceptors(HandlerInterceptor... interceptors) Add interceptors mapped to all incoming requests.addMappedInterceptors(String @Nullable [] pathPatterns, HandlerInterceptor... interceptors) Add interceptors mapped to a set of path patterns.addPlaceholderValue(String name, String value) In a standalone setup there is no support for placeholder values embedded in request mappings.extendMvcSingletons(@Nullable ServletContext servletContext) This method could be used from a subclass to register additional Spring MVC infrastructure such as additionalHandlerMapping,HandlerAdapter, and others.protected WebApplicationContextA method to obtain theWebApplicationContextto be passed to theDispatcherServlet.setAsyncRequestTimeout(long timeout) Specify the timeout value for async execution.Set a ContentNegotiationManager.setControllerAdvice(Object... controllerAdvice) Register one or moreControllerAdviceinstances to be used in tests (specifiedClasswill be turned into instance).setConversionService(FormattingConversionService conversionService) Provide a conversion service with custom formatters and converters.setCustomArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers) Provide custom resolvers for controller method arguments.Configure factory to create a customRequestMappingHandlerMapping.Provide custom handlers for controller method return values.setFlashMapManager(FlashMapManager flashMapManager) Provide a custom FlashMapManager instance.setHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) Set the HandlerExceptionResolver types to use as a list.setHandlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers) Set the HandlerExceptionResolver types to use as an array.setLocaleResolver(LocaleResolver localeResolver) Provide a LocaleResolver instance.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.Configure the parser to use forPathPatterns.setRemoveSemicolonContent(boolean removeSemicolonContent) Deprecated, for removal: This API element is subject to removal in a future version.setSingleView(View view) Sets up a singleViewResolverthat always returns the provided view instance.setValidator(Validator validator) Provide a customValidatorinstead of the one created by default.setViewResolvers(ViewResolver... resolvers) Set up view resolution with the givenViewResolvers.Methods inherited from class org.springframework.test.web.servlet.setup.AbstractMockMvcBuilderaddDispatcherServletCustomizer, addFilter, addFilter, addFilters, alwaysDo, alwaysExpect, apply, build, defaultRequest, defaultResponseCharacterEncoding, dispatchOptions, selfMethods inherited from class org.springframework.test.web.servlet.MockMvcBuilderSupportcreateMockMvc, createMockMvc
- 
Constructor Details- 
StandaloneMockMvcBuilderProtected constructor. Not intended for direct instantiation.- See Also:
 
 
- 
- 
Method Details- 
setControllerAdviceRegister one or moreControllerAdviceinstances to be used in tests (specifiedClasswill be turned into instance).Normally @ControllerAdviceare 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.- Since:
- 4.2
 
- 
setMessageConvertersSet 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. If no message converters are added to the list, a default list of converters is added instead.
- 
setValidatorProvide a customValidatorinstead of the one created by default. The default implementation used, assuming JSR-303 is on the classpath, isLocalValidatorFactoryBean.
- 
setConversionServiceProvide a conversion service with custom formatters and converters. If not set, aDefaultFormattingConversionServiceis used by default.
- 
addInterceptorsAdd interceptors mapped to all incoming requests.
- 
addMappedInterceptorspublic StandaloneMockMvcBuilder addMappedInterceptors(String @Nullable [] pathPatterns, HandlerInterceptor... interceptors) Add interceptors mapped to a set of path patterns.
- 
setContentNegotiationManagerSet a ContentNegotiationManager.
- 
setAsyncRequestTimeoutSpecify the timeout value for async execution. In Spring MVC Test, this value is used to determine how to long to wait for async execution to complete so that a test can verify the results synchronously.- Parameters:
- timeout- the timeout value in milliseconds
 
- 
setCustomArgumentResolverspublic StandaloneMockMvcBuilder setCustomArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers) Provide custom resolvers for controller method arguments.
- 
setCustomReturnValueHandlerspublic StandaloneMockMvcBuilder setCustomReturnValueHandlers(HandlerMethodReturnValueHandler... handlers) Provide custom handlers for controller method return values.
- 
setHandlerExceptionResolverspublic StandaloneMockMvcBuilder setHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) Set the HandlerExceptionResolver types to use as a list.
- 
setHandlerExceptionResolverspublic StandaloneMockMvcBuilder setHandlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers) Set the HandlerExceptionResolver types to use as an array.
- 
setViewResolversSet up view resolution with the givenViewResolvers. If not set, anInternalResourceViewResolveris used by default.
- 
setSingleViewSets up a singleViewResolverthat always returns the provided view instance. This is a convenient shortcut if you need to use one View instance only -- for example, rendering generated content (JSON, XML, Atom).
- 
setLocaleResolverProvide a LocaleResolver instance. If not provided, the default one used isAcceptHeaderLocaleResolver.
- 
setFlashMapManagerProvide a custom FlashMapManager instance. If not provided,SessionFlashMapManageris used by default.
- 
setPatternParserConfigure the parser to use forPathPatterns.By default, this is a default instance of PathPatternParser.- Parameters:
- parser- the parser to use
- Since:
- 5.3
 
- 
setRemoveSemicolonContent@Deprecated(since="7.0", forRemoval=true) public StandaloneMockMvcBuilder setRemoveSemicolonContent(boolean removeSemicolonContent) Deprecated, for removal: This API element is subject to removal in a future version.use ofPathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.Set if ";" (semicolon) content should be stripped from the request URI. The value, if provided, is in turn set onUrlPathHelper.setRemoveSemicolonContent(boolean).
- 
addPlaceholderValueIn a standalone setup there is no support for placeholder values embedded in request mappings. This method allows manually provided placeholder values so they can be resolved. Alternatively consider creating a test that initializes aWebApplicationContext.- Since:
- 4.2.8
 
- 
setCustomHandlerMappingpublic StandaloneMockMvcBuilder setCustomHandlerMapping(Supplier<RequestMappingHandlerMapping> factory) Configure factory to create a customRequestMappingHandlerMapping.- Parameters:
- factory- the factory
- Since:
- 5.0
 
- 
initWebAppContextDescription copied from class:AbstractMockMvcBuilderA method to obtain theWebApplicationContextto be passed to theDispatcherServlet. Invoked fromAbstractMockMvcBuilder.build()before theMockMvcinstance is created.- Specified by:
- initWebAppContextin class- AbstractMockMvcBuilder<StandaloneMockMvcBuilder>
 
- 
extendMvcSingletonsThis method could be used from a subclass to register additional Spring MVC infrastructure such as additionalHandlerMapping,HandlerAdapter, and others.- Parameters:
- servletContext- the ServletContext
- Returns:
- a map with additional MVC infrastructure object instances
- Since:
- 5.1.4
 
 
- 
PathMatcherandUrlPathHelperis deprecated for use at runtime in web modules in favor of parsed patterns withPathPatternParser.