Class StandaloneMockMvcBuilder

All Implemented Interfaces:
MockMvcBuilder, ConfigurableMockMvcBuilder<StandaloneMockMvcBuilder>

public class StandaloneMockMvcBuilder extends AbstractMockMvcBuilder<StandaloneMockMvcBuilder>
A 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 Details

  • Method Details

    • setControllerAdvice

      public StandaloneMockMvcBuilder setControllerAdvice(Object... controllerAdvice)
      Register one or more 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.

      Since:
      4.2
    • setMessageConverters

      public StandaloneMockMvcBuilder setMessageConverters(org.springframework.http.converter.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(org.springframework.validation.Validator validator)
      Provide a custom Validator instead of the one created by default. The default implementation used, assuming JSR-303 is on the classpath, is LocalValidatorFactoryBean.
    • setConversionService

      public StandaloneMockMvcBuilder setConversionService(org.springframework.format.support.FormattingConversionService conversionService)
      Provide a conversion service with custom formatters and converters. If not set, a DefaultFormattingConversionService is used by default.
    • addInterceptors

      public StandaloneMockMvcBuilder addInterceptors(org.springframework.web.servlet.HandlerInterceptor... interceptors)
      Add interceptors mapped to all incoming requests.
    • addMappedInterceptors

      public StandaloneMockMvcBuilder addMappedInterceptors(@Nullable String[] pathPatterns, org.springframework.web.servlet.HandlerInterceptor... interceptors)
      Add interceptors mapped to a set of path patterns.
    • setContentNegotiationManager

      public StandaloneMockMvcBuilder setContentNegotiationManager(org.springframework.web.accept.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.
      Parameters:
      timeout - the timeout value in milliseconds
    • setCustomArgumentResolvers

      public StandaloneMockMvcBuilder setCustomArgumentResolvers(org.springframework.web.method.support.HandlerMethodArgumentResolver... argumentResolvers)
      Provide custom resolvers for controller method arguments.
    • setCustomReturnValueHandlers

      public StandaloneMockMvcBuilder setCustomReturnValueHandlers(org.springframework.web.method.support.HandlerMethodReturnValueHandler... handlers)
      Provide custom handlers for controller method return values.
    • setHandlerExceptionResolvers

      public StandaloneMockMvcBuilder setHandlerExceptionResolvers(List<org.springframework.web.servlet.HandlerExceptionResolver> exceptionResolvers)
      Set the HandlerExceptionResolver types to use as a list.
    • setHandlerExceptionResolvers

      public StandaloneMockMvcBuilder setHandlerExceptionResolvers(org.springframework.web.servlet.HandlerExceptionResolver... exceptionResolvers)
      Set the HandlerExceptionResolver types to use as an array.
    • setViewResolvers

      public StandaloneMockMvcBuilder setViewResolvers(org.springframework.web.servlet.ViewResolver... resolvers)
      Set up view resolution with the given ViewResolvers. If not set, an InternalResourceViewResolver is used by default.
    • setSingleView

      public StandaloneMockMvcBuilder setSingleView(org.springframework.web.servlet.View view)
      Sets up a single 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).
    • setLocaleResolver

      public StandaloneMockMvcBuilder setLocaleResolver(org.springframework.web.servlet.LocaleResolver localeResolver)
      Provide a LocaleResolver instance. If not provided, the default one used is AcceptHeaderLocaleResolver.
    • setFlashMapManager

      public StandaloneMockMvcBuilder setFlashMapManager(org.springframework.web.servlet.FlashMapManager flashMapManager)
      Provide a custom FlashMapManager instance. If not provided, SessionFlashMapManager is used by default.
    • setPatternParser

      public StandaloneMockMvcBuilder setPatternParser(@Nullable org.springframework.web.util.pattern.PathPatternParser parser)
      Enable URL path matching with parsed PathPatterns instead of String pattern matching with a PathMatcher.
      Parameters:
      parser - the parser to use
      Since:
      5.3
    • setUseSuffixPatternMatch

      @Deprecated public StandaloneMockMvcBuilder setUseSuffixPatternMatch(boolean useSuffixPatternMatch)
      Deprecated.
      as of 5.2.4. See class-level note in RequestMappingHandlerMapping on 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 false.

    • setUseTrailingSlashPatternMatch

      @Deprecated(since="6.0") public StandaloneMockMvcBuilder setUseTrailingSlashPatternMatch(boolean useTrailingSlashPatternMatch)
      Deprecated.
      as of 6.0, see PathPatternParser.setMatchOptionalTrailingSeparator(boolean)
      Whether to match to URLs irrespective of the presence of a trailing slash. If enabled a method mapped to "/users" also matches to "/users/".
    • 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 on UrlPathHelper.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 a WebApplicationContext.
      Since:
      4.2.8
    • setCustomHandlerMapping

      public StandaloneMockMvcBuilder setCustomHandlerMapping(Supplier<org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping> factory)
      Configure factory to create a custom RequestMappingHandlerMapping.
      Parameters:
      factory - the factory
      Since:
      5.0
    • initWebAppContext

      protected org.springframework.web.context.WebApplicationContext initWebAppContext()
      Description copied from class: AbstractMockMvcBuilder
      A method to obtain the WebApplicationContext to be passed to the DispatcherServlet. Invoked from AbstractMockMvcBuilder.build() before the MockMvc instance is created.
      Specified by:
      initWebAppContext in class AbstractMockMvcBuilder<StandaloneMockMvcBuilder>
    • extendMvcSingletons

      protected Map<String,Object> extendMvcSingletons(@Nullable ServletContext servletContext)
      This method could be used from a subclass to register additional Spring MVC infrastructure such as additional HandlerMapping, HandlerAdapter, and others.
      Parameters:
      servletContext - the ServletContext
      Returns:
      a map with additional MVC infrastructure object instances
      Since:
      5.1.4