类 StandaloneMockMvcBuilder
- java.lang.Object
- org.springframework.test.web.servlet.MockMvcBuilderSupport
- org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder<StandaloneMockMvcBuilder>
- org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder
public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneMockMvcBuilder>
AMockMvcBuilderthat accepts@Controllerregistrations 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
DispatcherServletto 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 ofViewResolvers (seesetViewResolvers(ViewResolver...)).- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev
构造器概要
构造器 限定符 构造器 说明 protectedStandaloneMockMvcBuilder(Object... controllers)Protected constructor.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 StandaloneMockMvcBuilderaddInterceptors(HandlerInterceptor... interceptors)Add interceptors mapped to all incoming requests.StandaloneMockMvcBuilderaddMappedInterceptors(String[] pathPatterns, HandlerInterceptor... interceptors)Add interceptors mapped to a set of path patterns.StandaloneMockMvcBuilderaddPlaceholderValue(String name, String value)In a standalone setup there is no support for placeholder values embedded in request mappings.protected Map<String,Object>extendMvcSingletons(ServletContext servletContext)This method could be used from a sub-class to register additional Spring MVC infrastructure such as additionalHandlerMapping,HandlerAdapter, and others.protected WebApplicationContextinitWebAppContext()A method to obtain theWebApplicationContextto be passed to theDispatcherServlet.StandaloneMockMvcBuildersetAsyncRequestTimeout(long timeout)Specify the timeout value for async execution.StandaloneMockMvcBuildersetContentNegotiationManager(ContentNegotiationManager manager)Set a ContentNegotiationManager.StandaloneMockMvcBuildersetControllerAdvice(Object... controllerAdvice)Register one or moreControllerAdviceinstances to be used in tests (specifiedClasswill be turned into instance).StandaloneMockMvcBuildersetConversionService(FormattingConversionService conversionService)Provide a conversion service with custom formatters and converters.StandaloneMockMvcBuildersetCustomArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers)Provide custom resolvers for controller method arguments.StandaloneMockMvcBuildersetCustomHandlerMapping(Supplier<RequestMappingHandlerMapping> factory)Configure factory to create a customRequestMappingHandlerMapping.StandaloneMockMvcBuildersetCustomReturnValueHandlers(HandlerMethodReturnValueHandler... handlers)Provide custom handlers for controller method return values.StandaloneMockMvcBuildersetFlashMapManager(FlashMapManager flashMapManager)Provide a custom FlashMapManager instance.StandaloneMockMvcBuildersetHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers)Set the HandlerExceptionResolver types to use as a list.StandaloneMockMvcBuildersetHandlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers)Set the HandlerExceptionResolver types to use as an array.StandaloneMockMvcBuildersetLocaleResolver(LocaleResolver localeResolver)Provide a LocaleResolver instance.StandaloneMockMvcBuildersetMessageConverters(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.StandaloneMockMvcBuildersetRemoveSemicolonContent(boolean removeSemicolonContent)Set if ";" (semicolon) content should be stripped from the request URI.StandaloneMockMvcBuildersetSingleView(View view)Sets up a singleViewResolverthat always returns the provided view instance.StandaloneMockMvcBuildersetUseSuffixPatternMatch(boolean useSuffixPatternMatch)已过时。as of 5.2.4.StandaloneMockMvcBuildersetUseTrailingSlashPatternMatch(boolean useTrailingSlashPatternMatch)Whether to match to URLs irrespective of the presence of a trailing slash.StandaloneMockMvcBuildersetValidator(Validator validator)Provide a customValidatorinstead of the one created by default.StandaloneMockMvcBuildersetViewResolvers(ViewResolver... resolvers)Set up view resolution with the givenViewResolvers.从类继承的方法 org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder
addDispatcherServletCustomizer, addFilter, addFilters, alwaysDo, alwaysExpect, apply, build, defaultRequest, dispatchOptions, self
从类继承的方法 org.springframework.test.web.servlet.MockMvcBuilderSupport
createMockMvc
构造器详细资料
StandaloneMockMvcBuilder
protected StandaloneMockMvcBuilder(Object... controllers)
Protected constructor. Not intended for direct instantiation.
方法详细资料
setControllerAdvice
public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice)
Register 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.- 从以下版本开始:
- 4.2
setMessageConverters
public 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. If no message converters are added to the list, a default list of converters is added instead.
setValidator
public StandaloneMockMvcBuilder setValidator(Validator validator)
Provide a customValidatorinstead of the one created by default. The default implementation used, assuming JSR-303 is on the classpath, isLocalValidatorFactoryBean.
setConversionService
public StandaloneMockMvcBuilder setConversionService(FormattingConversionService conversionService)
Provide a conversion service with custom formatters and converters. If not set, aDefaultFormattingConversionServiceis used by default.
addInterceptors
public StandaloneMockMvcBuilder addInterceptors(HandlerInterceptor... interceptors)
Add interceptors mapped to all incoming requests.
addMappedInterceptors
public StandaloneMockMvcBuilder addMappedInterceptors(@Nullable String[] pathPatterns, HandlerInterceptor... interceptors)
Add interceptors mapped to a set of path patterns.
setContentNegotiationManager
public StandaloneMockMvcBuilder setContentNegotiationManager(ContentNegotiationManager manager)
Set a ContentNegotiationManager.
setAsyncRequestTimeout
public StandaloneMockMvcBuilder setAsyncRequestTimeout(long timeout)
Specify 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.- 参数:
timeout- the timeout value in milliseconds
setCustomArgumentResolvers
public StandaloneMockMvcBuilder setCustomArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers)
Provide custom resolvers for controller method arguments.
setCustomReturnValueHandlers
public StandaloneMockMvcBuilder setCustomReturnValueHandlers(HandlerMethodReturnValueHandler... handlers)
Provide custom handlers for controller method return values.
setHandlerExceptionResolvers
public StandaloneMockMvcBuilder setHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers)
Set the HandlerExceptionResolver types to use as a list.
setHandlerExceptionResolvers
public StandaloneMockMvcBuilder setHandlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers)
Set the HandlerExceptionResolver types to use as an array.
setViewResolvers
public StandaloneMockMvcBuilder setViewResolvers(ViewResolver... resolvers)
Set up view resolution with the givenViewResolvers. If not set, anInternalResourceViewResolveris used by default.
setSingleView
public StandaloneMockMvcBuilder setSingleView(View view)
Sets up a singleViewResolverthat 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).
setLocaleResolver
public StandaloneMockMvcBuilder setLocaleResolver(LocaleResolver localeResolver)
Provide a LocaleResolver instance. If not provided, the default one used isAcceptHeaderLocaleResolver.
setFlashMapManager
public StandaloneMockMvcBuilder setFlashMapManager(FlashMapManager flashMapManager)
Provide a custom FlashMapManager instance. If not provided,SessionFlashMapManageris used by default.
setUseSuffixPatternMatch
@Deprecated public StandaloneMockMvcBuilder setUseSuffixPatternMatch(boolean useSuffixPatternMatch)
已过时。as of 5.2.4. See class-level note inRequestMappingHandlerMappingon the deprecation of path extension config options.Whether to use suffix pattern match (".*") when matching patterns to requests. If enabled a method mapped to "/users" also matches to "/users.*".The default value is
true.
setUseTrailingSlashPatternMatch
public StandaloneMockMvcBuilder setUseTrailingSlashPatternMatch(boolean useTrailingSlashPatternMatch)
Whether to match to URLs irrespective of the presence of a trailing slash. If enabled a method mapped to "/users" also matches to "/users/".The default value is
true.
setRemoveSemicolonContent
public StandaloneMockMvcBuilder setRemoveSemicolonContent(boolean removeSemicolonContent)
Set if ";" (semicolon) content should be stripped from the request URI. The value, if provided, is in turn set onAbstractHandlerMapping.setRemoveSemicolonContent(boolean).
addPlaceholderValue
public StandaloneMockMvcBuilder addPlaceholderValue(String name, String value)
In 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.- 从以下版本开始:
- 4.2.8
setCustomHandlerMapping
public StandaloneMockMvcBuilder setCustomHandlerMapping(Supplier<RequestMappingHandlerMapping> factory)
Configure factory to create a customRequestMappingHandlerMapping.- 参数:
factory- the factory- 从以下版本开始:
- 5.0
initWebAppContext
protected WebApplicationContext initWebAppContext()
从类复制的说明:AbstractMockMvcBuilderA method to obtain theWebApplicationContextto be passed to theDispatcherServlet. Invoked fromAbstractMockMvcBuilder.build()before theMockMvcinstance is created.
extendMvcSingletons
protected Map<String,Object> extendMvcSingletons(@Nullable ServletContext servletContext)
This method could be used from a sub-class to register additional Spring MVC infrastructure such as additionalHandlerMapping,HandlerAdapter, and others.- 参数:
servletContext- the ServletContext- 返回:
- a map with additional MVC infrastructure object instances
- 从以下版本开始:
- 5.1.4