spring-framework / org.springframework.test.web.servlet.setup / ConfigurableMockMvcBuilder

ConfigurableMockMvcBuilder

interface ConfigurableMockMvcBuilder<B : ConfigurableMockMvcBuilder<B>> : MockMvcBuilder

Defines common methods for building a MockMvc.

Author
Rossen Stoyanchev

Since
4.1

Functions

addFilter

abstract fun <T : B> addFilter(filter: Filter, vararg urlPatterns: String): T

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.

addFilters

abstract fun <T : B> addFilters(vararg filters: Filter): T

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.

alwaysDo

abstract fun <T : B> alwaysDo(resultHandler: ResultHandler): T

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.

alwaysExpect

abstract fun <T : B> alwaysExpect(resultMatcher: ResultMatcher): T

Define a global expectation that should always be applied to every response. For example, status code 200 (OK), content type "application/json", etc.

apply

abstract fun <T : B> apply(configurer: MockMvcConfigurer): T

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.

defaultRequest

abstract fun <T : B> defaultRequest(requestBuilder: RequestBuilder): T

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.

dispatchOptions

abstract fun <T : B> dispatchOptions(dispatchOptions: Boolean): T

Whether to enable the DispatcherServlet property dispatchOptionsRequest which allows processing of HTTP OPTIONS requests.