Class RouterFunctionMockMvcBuilder

All Implemented Interfaces:
MockMvcBuilder, ConfigurableMockMvcBuilder<RouterFunctionMockMvcBuilder>

public class RouterFunctionMockMvcBuilder extends AbstractMockMvcBuilder<RouterFunctionMockMvcBuilder>
A MockMvcBuilder that accepts RouterFunction registrations thus allowing full control over the instantiation and initialization of router functions and their dependencies similar to plain unit tests, and also making it possible to test one function at a time.

This builder creates the minimum infrastructure required by the DispatcherServlet to serve requests with router functions 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:
6.2
Author:
Arjen Poutsma
  • Constructor Details

    • RouterFunctionMockMvcBuilder

      protected RouterFunctionMockMvcBuilder(org.springframework.web.servlet.function.RouterFunction<?>... routerFunctions)
  • Method Details

    • setMessageConverters

      public RouterFunctionMockMvcBuilder 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.
    • addInterceptors

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

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

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

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

      public RouterFunctionMockMvcBuilder setCustomHandlerMapping(Supplier<org.springframework.web.servlet.function.support.RouterFunctionMapping> factory)
      Configure the factory to create a custom RequestMappingHandlerMapping.
      Parameters:
      factory - the factory
    • setViewResolvers

      public RouterFunctionMockMvcBuilder 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 RouterFunctionMockMvcBuilder setSingleView(org.springframework.web.servlet.View view)
      Set 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 — for example, rendering generated content (JSON, XML, Atom).

    • setAsyncRequestTimeout

      public RouterFunctionMockMvcBuilder setAsyncRequestTimeout(long timeout)
      Specify the timeout value for async execution.

      In Spring MVC Test, this value is used to determine how long to wait for async execution to complete so that a test can verify the results synchronously.

      Parameters:
      timeout - the timeout value in milliseconds
    • setPatternParser

      public RouterFunctionMockMvcBuilder 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
    • 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<RouterFunctionMockMvcBuilder>