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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic RequestBuilder
asyncDispatch
(MvcResult mvcResult) Create aRequestBuilder
for an async dispatch from theMvcResult
of the request that started async processing.Create aMockHttpServletRequestBuilder
for a DELETE request.Create aMockHttpServletRequestBuilder
for a DELETE request.Create aMockHttpServletRequestBuilder
for a GET request.Create aMockHttpServletRequestBuilder
for a GET request.Create aMockHttpServletRequestBuilder
for a HEAD request.Create aMockHttpServletRequestBuilder
for a HEAD request.Create aMockMultipartHttpServletRequestBuilder
for a multipart request.Create aMockMultipartHttpServletRequestBuilder
for a multipart request.Create aMockHttpServletRequestBuilder
for an OPTIONS request.Create aMockHttpServletRequestBuilder
for an OPTIONS request.Create aMockHttpServletRequestBuilder
for a PATCH request.Create aMockHttpServletRequestBuilder
for a PATCH request.Create aMockHttpServletRequestBuilder
for a POST request.Create aMockHttpServletRequestBuilder
for a POST request.Create aMockHttpServletRequestBuilder
for a PUT request.Create aMockHttpServletRequestBuilder
for a PUT request.Alternative factory method that allows for custom HTTP verbs (e.g.request
(HttpMethod method, String urlTemplate, Object... uriVars) Create aMockHttpServletRequestBuilder
for a request with the given HTTP method.request
(HttpMethod httpMethod, URI uri) Create aMockHttpServletRequestBuilder
for a request with the given HTTP method.
-
Constructor Details
-
MockMvcRequestBuilders
public MockMvcRequestBuilders()
-
-
Method Details
-
get
Create aMockHttpServletRequestBuilder
for a GET request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables
-
get
Create aMockHttpServletRequestBuilder
for a GET request.- Parameters:
uri
- the URL- Since:
- 4.0.3
-
post
Create aMockHttpServletRequestBuilder
for a POST request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables
-
post
Create aMockHttpServletRequestBuilder
for a POST request.- Parameters:
uri
- the URL- Since:
- 4.0.3
-
put
Create aMockHttpServletRequestBuilder
for a PUT request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables
-
put
Create aMockHttpServletRequestBuilder
for a PUT request.- Parameters:
uri
- the URL- Since:
- 4.0.3
-
patch
Create aMockHttpServletRequestBuilder
for a PATCH request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables
-
patch
Create aMockHttpServletRequestBuilder
for a PATCH request.- Parameters:
uri
- the URL- Since:
- 4.0.3
-
delete
Create aMockHttpServletRequestBuilder
for a DELETE request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables
-
delete
Create aMockHttpServletRequestBuilder
for a DELETE request.- Parameters:
uri
- the URL- Since:
- 4.0.3
-
options
Create aMockHttpServletRequestBuilder
for an OPTIONS request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables
-
options
Create aMockHttpServletRequestBuilder
for an OPTIONS request.- Parameters:
uri
- the URL- Since:
- 4.0.3
-
head
Create aMockHttpServletRequestBuilder
for a HEAD request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Since:
- 4.1
-
head
Create aMockHttpServletRequestBuilder
for a HEAD request.- Parameters:
uri
- the URL- Since:
- 4.1
-
request
public static MockHttpServletRequestBuilder request(HttpMethod method, String urlTemplate, Object... uriVars) Create aMockHttpServletRequestBuilder
for a request with the given HTTP method.- Parameters:
method
- the HTTP method (GET, POST, etc)urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables
-
request
Create aMockHttpServletRequestBuilder
for a request with the given HTTP method.- Parameters:
httpMethod
- the HTTP method (GET, POST, etc)uri
- the URL- Since:
- 4.0.3
-
request
Alternative factory method that allows for custom HTTP verbs (e.g. WebDAV).- Parameters:
httpMethod
- the HTTP methoduri
- the URL- Since:
- 4.3
-
multipart
public static MockMultipartHttpServletRequestBuilder multipart(String urlTemplate, Object... uriVars) Create aMockMultipartHttpServletRequestBuilder
for a multipart request.- Parameters:
urlTemplate
- a URL template; the resulting URL will be encodeduriVars
- zero or more URI variables- Since:
- 5.0
-
multipart
Create aMockMultipartHttpServletRequestBuilder
for a multipart request.- Parameters:
uri
- the URL- Since:
- 5.0
-
asyncDispatch
Create aRequestBuilder
for an async dispatch from theMvcResult
of 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
-