Class MockMvcRequestBuilders
java.lang.Object
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
Static factory methods for 
RequestBuilders.
Integration with the Spring TestContext Framework
Methods in this class will reuse a
MockServletContext
that was created by the Spring TestContext Framework.
Eclipse Users
Consider adding this class as a Java editor favorite. To navigate to this setting, open the Preferences and type "favorites".
- Since:
- 3.2
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Greg Turnquist, Sebastien Deleuze, Sam Brannen, Kamill Sokol
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic RequestBuilderasyncDispatch(MvcResult mvcResult) Create aRequestBuilderfor an async dispatch from theMvcResultof the request that started async processing.Create aMockHttpServletRequestBuilderfor a DELETE request.Create aMockHttpServletRequestBuilderfor a DELETE request.Create aMockHttpServletRequestBuilderfor a GET request.Create aMockHttpServletRequestBuilderfor a GET request.Create aMockHttpServletRequestBuilderfor a HEAD request.Create aMockHttpServletRequestBuilderfor a HEAD request.Create aMockMultipartHttpServletRequestBuilderfor a multipart request, using POST as the HTTP method.Variant ofmultipart(String, Object...)with aURI.multipart(HttpMethod httpMethod, String uriTemplate, @Nullable Object... uriVariables) Variant ofmultipart(String, Object...)that also accepts anHttpMethod.multipart(HttpMethod httpMethod, URI uri) Create aMockHttpServletRequestBuilderfor an OPTIONS request.Create aMockHttpServletRequestBuilderfor an OPTIONS request.Create aMockHttpServletRequestBuilderfor a PATCH request.Create aMockHttpServletRequestBuilderfor a PATCH request.Create aMockHttpServletRequestBuilderfor a POST request.Create aMockHttpServletRequestBuilderfor a POST request.Create aMockHttpServletRequestBuilderfor a PUT request.Create aMockHttpServletRequestBuilderfor a PUT request.Deprecated.request(HttpMethod method, String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a request with the given HTTP method.request(HttpMethod httpMethod, URI uri) Create aMockHttpServletRequestBuilderfor a request with the given HTTP method.
- 
Constructor Details- 
MockMvcRequestBuilderspublic MockMvcRequestBuilders()
 
- 
- 
Method Details- 
getpublic static MockHttpServletRequestBuilder get(String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a GET request.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
 
- 
getCreate aMockHttpServletRequestBuilderfor a GET request.- Parameters:
- uri- the URI
- Since:
- 4.0.3
 
- 
postpublic static MockHttpServletRequestBuilder post(String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a POST request.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
 
- 
postCreate aMockHttpServletRequestBuilderfor a POST request.- Parameters:
- uri- the URI
- Since:
- 4.0.3
 
- 
putpublic static MockHttpServletRequestBuilder put(String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a PUT request.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
 
- 
putCreate aMockHttpServletRequestBuilderfor a PUT request.- Parameters:
- uri- the URI
- Since:
- 4.0.3
 
- 
patchpublic static MockHttpServletRequestBuilder patch(String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a PATCH request.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
 
- 
patchCreate aMockHttpServletRequestBuilderfor a PATCH request.- Parameters:
- uri- the URI
- Since:
- 4.0.3
 
- 
deletepublic static MockHttpServletRequestBuilder delete(String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a DELETE request.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
 
- 
deleteCreate aMockHttpServletRequestBuilderfor a DELETE request.- Parameters:
- uri- the URI
- Since:
- 4.0.3
 
- 
optionspublic static MockHttpServletRequestBuilder options(String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor an OPTIONS request.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
 
- 
optionsCreate aMockHttpServletRequestBuilderfor an OPTIONS request.- Parameters:
- uri- the URI
- Since:
- 4.0.3
 
- 
headpublic static MockHttpServletRequestBuilder head(String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a HEAD request.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
- Since:
- 4.1
 
- 
headCreate aMockHttpServletRequestBuilderfor a HEAD request.- Parameters:
- uri- the URI
- Since:
- 4.1
 
- 
requestpublic static MockHttpServletRequestBuilder request(HttpMethod method, String uriTemplate, @Nullable Object... uriVariables) Create aMockHttpServletRequestBuilderfor a request with the given HTTP method.- Parameters:
- method- the HTTP method (GET, POST, etc.)
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
 
- 
requestCreate aMockHttpServletRequestBuilderfor a request with the given HTTP method.- Parameters:
- httpMethod- the HTTP method (GET, POST, etc.)
- uri- the URI
- Since:
- 4.0.3
 
- 
request@Deprecated(since="6.2") public static MockHttpServletRequestBuilder request(String httpMethod, URI uri) Deprecated.in favor ofrequest(HttpMethod, URI)Alternative factory method that allows for custom HTTP verbs (for example, WebDAV).- Parameters:
- httpMethod- the HTTP method
- uri- the URI
- Since:
- 4.3
 
- 
multipartpublic static MockMultipartHttpServletRequestBuilder multipart(String uriTemplate, @Nullable Object... uriVariables) Create aMockMultipartHttpServletRequestBuilderfor a multipart request, using POST as the HTTP method.- Parameters:
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
- Since:
- 5.0
 
- 
multipartpublic static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, String uriTemplate, @Nullable Object... uriVariables) Variant ofmultipart(String, Object...)that also accepts anHttpMethod.- Parameters:
- httpMethod- the HTTP method to use
- uriTemplate- a URI template; the resulting URI will be encoded
- uriVariables- zero or more URI variables
- Since:
- 5.3.22
 
- 
multipartVariant ofmultipart(String, Object...)with aURI.- Parameters:
- uri- the URI
- Since:
- 5.0
 
- 
multipart- Parameters:
- httpMethod- the HTTP method to use
- uri- the URI
- Since:
- 5.3.21
 
- 
asyncDispatchCreate aRequestBuilderfor an async dispatch from theMvcResultof the request that started async processing.Usage involves performing a request that starts async processing first: MvcResult mvcResult = this.mockMvc.perform(get("/1")) .andExpect(request().asyncStarted()) .andReturn();And then performing the async dispatch re-using the MvcResult:this.mockMvc.perform(asyncDispatch(mvcResult)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));- Parameters:
- mvcResult- the result from the request that started async processing
 
 
- 
request(HttpMethod, URI)