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
Constructors -
Method Summary
Modifier 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, 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, 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
-
MockMvcRequestBuilders
public MockMvcRequestBuilders()
-
-
Method Details
-
get
Create aMockHttpServletRequestBuilderfor a GET request.- Parameters:
uriTemplate- a URI template; the resulting URI will be encodeduriVariables- zero or more URI variables
-
get
Create aMockHttpServletRequestBuilderfor a GET request.- Parameters:
uri- the URI- Since:
- 4.0.3
-
post
Create aMockHttpServletRequestBuilderfor a POST request.- Parameters:
uriTemplate- a URI template; the resulting URI will be encodeduriVariables- zero or more URI variables
-
post
Create aMockHttpServletRequestBuilderfor a POST request.- Parameters:
uri- the URI- Since:
- 4.0.3
-
put
Create aMockHttpServletRequestBuilderfor a PUT request.- Parameters:
uriTemplate- a URI template; the resulting URI will be encodeduriVariables- zero or more URI variables
-
put
Create aMockHttpServletRequestBuilderfor a PUT request.- Parameters:
uri- the URI- Since:
- 4.0.3
-
patch
Create aMockHttpServletRequestBuilderfor a PATCH request.- Parameters:
uriTemplate- a URI template; the resulting URI will be encodeduriVariables- zero or more URI variables
-
patch
Create aMockHttpServletRequestBuilderfor a PATCH request.- Parameters:
uri- the URI- Since:
- 4.0.3
-
delete
Create aMockHttpServletRequestBuilderfor a DELETE request.- Parameters:
uriTemplate- a URI template; the resulting URI will be encodeduriVariables- zero or more URI variables
-
delete
Create aMockHttpServletRequestBuilderfor a DELETE request.- Parameters:
uri- the URI- Since:
- 4.0.3
-
options
Create aMockHttpServletRequestBuilderfor an OPTIONS request.- Parameters:
uriTemplate- a URI template; the resulting URI will be encodeduriVariables- zero or more URI variables
-
options
Create aMockHttpServletRequestBuilderfor an OPTIONS request.- Parameters:
uri- the URI- Since:
- 4.0.3
-
head
Create aMockHttpServletRequestBuilderfor a HEAD request.- Parameters:
uriTemplate- a URI template; the resulting URI will be encodeduriVariables- zero or more URI variables- Since:
- 4.1
-
head
Create aMockHttpServletRequestBuilderfor a HEAD request.- Parameters:
uri- the URI- Since:
- 4.1
-
request
public static MockHttpServletRequestBuilder request(HttpMethod method, String uriTemplate, 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 encodeduriVariables- zero or more URI variables
-
request
Create 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 methoduri- the URI- Since:
- 4.3
-
multipart
public static MockMultipartHttpServletRequestBuilder multipart(String uriTemplate, Object... uriVariables) Create aMockMultipartHttpServletRequestBuilderfor 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 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)