Interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>>

Type Parameters:
B - a self reference to the builder type
All Superinterfaces:
MockMvcBuilder
All Known Implementing Classes:
AbstractMockMvcBuilder, DefaultMockMvcBuilder, StandaloneMockMvcBuilder

public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>> extends MockMvcBuilder
Defines common methods for building a MockMvc.
Since:
4.1
Author:
Rossen Stoyanchev, Sam Brannen
  • Method Summary

    Modifier and Type
    Method
    Description
    <T extends B>
    T
    A more advanced variant of dispatchOptions(boolean) that allows customizing any DispatcherServlet property.
    <T extends B>
    T
    addFilter(Filter filter, String... urlPatterns)
    Add a filter mapped to a specific set of patterns.
    <T extends B>
    T
    addFilters(Filter... filters)
    Add filters mapped to any request (i.e.
    <T extends B>
    T
    alwaysDo(ResultHandler resultHandler)
    Define a global action that should always be applied to every response.
    <T extends B>
    T
    alwaysExpect(ResultMatcher resultMatcher)
    Define a global expectation that should always be applied to every response.
    <T extends B>
    T
    Add a MockMvcConfigurer that automates MockMvc setup and configures it for some specific purpose (e.g.
    <T extends B>
    T
    Define default request properties that should be merged into all performed requests.
    default <T extends B>
    T
    defaultResponseCharacterEncoding(Charset defaultResponseCharacterEncoding)
    Define the default character encoding to be applied to every response.
    <T extends B>
    T
    dispatchOptions(boolean dispatchOptions)
    Whether to enable the DispatcherServlet property dispatchOptionsRequest which allows processing of HTTP OPTIONS requests.

    Methods inherited from interface org.springframework.test.web.servlet.MockMvcBuilder

    build
  • Method Details

    • addFilters

      <T extends B> T addFilters(Filter... filters)
      Add filters mapped to any request (i.e. "/*"). For example:
       mockMvcBuilder.addFilters(springSecurityFilterChain);
       

      It is the equivalent of the following web.xml configuration:

       <filter-mapping>
           <filter-name>springSecurityFilterChain</filter-name>
           <url-pattern>/*</url-pattern>
       </filter-mapping>
       

      Filters will be invoked in the order in which they are provided.

      Parameters:
      filters - the filters to add
    • addFilter

      <T extends B> T addFilter(Filter filter, String... urlPatterns)
      Add a filter mapped to a specific set of patterns. For example:
       mockMvcBuilder.addFilter(myResourceFilter, "/resources/*");
       

      It is the equivalent of:

       <filter-mapping>
           <filter-name>myResourceFilter</filter-name>
           <url-pattern>/resources/*</url-pattern>
       </filter-mapping>
       

      Filters will be invoked in the order in which they are provided.

      Parameters:
      filter - the filter to add
      urlPatterns - the URL patterns to map to; if empty, "/*" is used by default
    • defaultRequest

      <T extends B> T defaultRequest(RequestBuilder requestBuilder)
      Define default request properties that should be merged into all performed requests. In effect this provides a mechanism for defining common initialization for all requests such as the content type, request parameters, session attributes, and any other request property.

      Properties specified at the time of performing a request override the default properties defined here.

      Parameters:
      requestBuilder - a RequestBuilder; see static factory methods in MockMvcRequestBuilders
    • defaultResponseCharacterEncoding

      default <T extends B> T defaultResponseCharacterEncoding(Charset defaultResponseCharacterEncoding)
      Define the default character encoding to be applied to every response.

      The default implementation of this method throws an UnsupportedOperationException. Concrete implementations are therefore encouraged to override this method.

      Parameters:
      defaultResponseCharacterEncoding - the default response character encoding
      Since:
      5.3.10
    • alwaysExpect

      <T extends B> T alwaysExpect(ResultMatcher resultMatcher)
      Define a global expectation that should always be applied to every response. For example, status code 200 (OK), content type "application/json", etc.
      Parameters:
      resultMatcher - a ResultMatcher; see static factory methods in MockMvcResultMatchers
    • alwaysDo

      <T extends B> T alwaysDo(ResultHandler resultHandler)
      Define a global action that should always be applied to every response. For example, writing detailed information about the performed request and resulting response to System.out.
      Parameters:
      resultHandler - a ResultHandler; see static factory methods in MockMvcResultHandlers
    • dispatchOptions

      <T extends B> T dispatchOptions(boolean dispatchOptions)
      Whether to enable the DispatcherServlet property dispatchOptionsRequest which allows processing of HTTP OPTIONS requests.
    • addDispatcherServletCustomizer

      <T extends B> T addDispatcherServletCustomizer(DispatcherServletCustomizer customizer)
      A more advanced variant of dispatchOptions(boolean) that allows customizing any DispatcherServlet property.
      Since:
      5.3
    • apply

      <T extends B> T apply(MockMvcConfigurer configurer)
      Add a MockMvcConfigurer that automates MockMvc setup and configures it for some specific purpose (e.g. security).

      There is a built-in SharedHttpSessionConfigurer that can be used to re-use the HTTP session across requests. 3rd party frameworks like Spring Security also use this mechanism to provide configuration shortcuts.

      See Also: