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, using POST as the HTTP method.Variant ofmultipart(String, Object...)
with aURI
.multipart
(HttpMethod httpMethod, String uriTemplate, Object... uriVariables) Variant ofmultipart(String, Object...)
that also accepts anHttpMethod
.multipart
(HttpMethod httpMethod, URI uri) 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.Deprecated.request
(HttpMethod method, String uriTemplate, Object... uriVariables) 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:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables
-
get
Create aMockHttpServletRequestBuilder
for a GET request.- Parameters:
uri
- the URI- Since:
- 4.0.3
-
post
Create aMockHttpServletRequestBuilder
for a POST request.- Parameters:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables
-
post
Create aMockHttpServletRequestBuilder
for a POST request.- Parameters:
uri
- the URI- Since:
- 4.0.3
-
put
Create aMockHttpServletRequestBuilder
for a PUT request.- Parameters:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables
-
put
Create aMockHttpServletRequestBuilder
for a PUT request.- Parameters:
uri
- the URI- Since:
- 4.0.3
-
patch
Create aMockHttpServletRequestBuilder
for a PATCH request.- Parameters:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables
-
patch
Create aMockHttpServletRequestBuilder
for a PATCH request.- Parameters:
uri
- the URI- Since:
- 4.0.3
-
delete
Create aMockHttpServletRequestBuilder
for a DELETE request.- Parameters:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables
-
delete
Create aMockHttpServletRequestBuilder
for a DELETE request.- Parameters:
uri
- the URI- Since:
- 4.0.3
-
options
Create aMockHttpServletRequestBuilder
for an OPTIONS request.- Parameters:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables
-
options
Create aMockHttpServletRequestBuilder
for an OPTIONS request.- Parameters:
uri
- the URI- Since:
- 4.0.3
-
head
Create aMockHttpServletRequestBuilder
for a HEAD request.- Parameters:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables- Since:
- 4.1
-
head
Create aMockHttpServletRequestBuilder
for a HEAD request.- Parameters:
uri
- the URI- Since:
- 4.1
-
request
public static MockHttpServletRequestBuilder request(HttpMethod method, String uriTemplate, Object... uriVariables) Create aMockHttpServletRequestBuilder
for a request with the given HTTP method.- Parameters:
method
- the HTTP method (GET, POST, etc.)uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- 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 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 (e.g. WebDAV).- Parameters:
httpMethod
- the HTTP methoduri
- the URI- Since:
- 4.3
-
multipart
public static MockMultipartHttpServletRequestBuilder multipart(String uriTemplate, Object... uriVariables) Create aMockMultipartHttpServletRequestBuilder
for a multipart request, using POST as the HTTP method.- Parameters:
uriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables- Since:
- 5.0
-
multipart
public static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, String uriTemplate, Object... uriVariables) Variant ofmultipart(String, Object...)
that also accepts anHttpMethod
.- Parameters:
httpMethod
- the HTTP method to useuriTemplate
- a URI template; the resulting URI will be encodeduriVariables
- zero or more URI variables- Since:
- 5.3.22
-
multipart
Variant ofmultipart(String, Object...)
with aURI
.- Parameters:
uri
- the URI- Since:
- 5.0
-
multipart
- Parameters:
httpMethod
- the HTTP method to useuri
- the URI- Since:
- 5.3.21
-
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
-
request(HttpMethod, URI)