org.springframework.test.web.servlet.setup

## Class DefaultMockMvcBuilder<Self extends MockMvcBuilder>

- java.lang.Object
- - org.springframework.test.web.servlet.MockMvcBuilderSupport
  - - org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder<Self>

All Implemented Interfaces:
  MockMvcBuilder

Direct Known Subclasses:
  StandaloneMockMvcBuilder

    public class DefaultMockMvcBuilder<Self extends MockMvcBuilder>
    extends MockMvcBuilderSupport
    implements MockMvcBuilder

An concrete implementation of `MockMvcBuilder` with methods for configuring filters, default request properties, and global expectations and result actions.

Since:
  3.2
Author:
  Rossen Stoyanchev, Rob Winch

### Constructor Summary

Constructors
`protected` `DefaultMockMvcBuilder(WebApplicationContext webAppContext)` Protected constructor.

### Method Summary

Methods
`<T extends Self> T` `addFilter(Filter filter, String... urlPatterns)` Add a filter mapped to a specific set of patterns.
`<T extends Self> T` `addFilters(Filter... filters)` Add filters mapped to any request (i.e.
`<T extends Self> T` `alwaysDo(ResultHandler resultHandler)` Define a global action that should always be applied to every response.
`<T extends Self> T` `alwaysExpect(ResultMatcher resultMatcher)` Define a global expectation that should always be applied to every response.
`MockMvc` `build()` Build a `MockMvc` instance.
`<T extends Self> T` `defaultRequest(RequestBuilder requestBuilder)` Define default request properties that should be merged into all performed requests.
`<T extends Self> T` `dispatchOptions(boolean dispatchOptions)` Should the `DispatcherServlet` dispatch OPTIONS request to controllers.
`protected void` `initWebAppContext(WebApplicationContext webAppContext)` Invoked from `build()` before the `MockMvc` instance is created.

### Methods inherited from class org.springframework.test.web.servlet.MockMvcBuilderSupport

`createMockMvc`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### DefaultMockMvcBuilder

    protected DefaultMockMvcBuilder(WebApplicationContext webAppContext)

Protected constructor. Not intended for direct instantiation.

See Also:
  `MockMvcBuilders.webAppContextSetup(WebApplicationContext)`

### Method Detail

#### addFilters

    public final <T extends Self> T addFilters(Filter... filters)

Add filters mapped to any request (i.e. "/*"). For example:

     mockMvcBuilder.addFilters(springSecurityFilterChain);

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

    public final <T extends Self> T addFilter(Filter filter,
                               String... urlPatterns)

Add a filter mapped to a specific set of patterns. For example:

     mockMvcBuilder.addFilters(myResourceFilter, "/resources/*");

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` - URL patterns to map to; if empty, "/*" is used by default

#### defaultRequest

    public final <T extends Self> 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` .

#### alwaysExpect

    public final <T extends Self> 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

    public final <T extends Self> 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

    public final <T extends Self> T dispatchOptions(boolean dispatchOptions)

Should the `DispatcherServlet` dispatch OPTIONS request to controllers.

Parameters:
  `dispatchOptions` -
See Also:
  `FrameworkServlet.setDispatchOptionsRequest(boolean)`

#### build

    public final MockMvc build()

Build a `MockMvc` instance.

Specified by:
  `build` in interface `MockMvcBuilder`

#### initWebAppContext

    protected void initWebAppContext(WebApplicationContext webAppContext)

Invoked from `build()` before the `MockMvc` instance is created. Allows sub-classes to further initialize the `WebApplicationContext` and the `javax.servlet.ServletContext` it contains.