public class MockMvcRequestSpecBuilder extends Object
MockMvcRequestSpecification requestSpec = new MockMvcRequestSpecBuilder().addParameter("parameter1", "value1").build();
given().
spec(requestSpec).
when().
get("/something").
then().
body("x.y.z", equalTo("something")).
| Constructor and Description |
|---|
MockMvcRequestSpecBuilder() |
| Modifier and Type | Method and Description |
|---|---|
MockMvcRequestSpecBuilder |
addAttribute(String attributeName,
Object attributeValue)
Add request attribute
|
MockMvcRequestSpecBuilder |
addAttributes(Map<String,?> attributesMap)
Add request attributes
|
MockMvcRequestSpecBuilder |
addCookie(io.restassured.http.Cookie cookie)
Add a detailed cookie
|
MockMvcRequestSpecBuilder |
addCookie(String key,
Object value,
Object... cookieNameValuePairs)
Add a cookie to be sent with the request.
|
MockMvcRequestSpecBuilder |
addCookies(Map<String,?> cookies)
Add cookies to be sent with the request as Map e.g:
|
MockMvcRequestSpecBuilder |
addFormParam(String parameterName,
Collection<?> parameterValues) |
MockMvcRequestSpecBuilder |
addFormParam(String parameterName,
Object... parameterValues) |
MockMvcRequestSpecBuilder |
addFormParams(Map<String,?> parametersMap) |
MockMvcRequestSpecBuilder |
addHeader(io.restassured.http.Header header)
Add a header to be sent with the request.
|
MockMvcRequestSpecBuilder |
addHeader(String headerName,
String headerValue)
Add a header to be sent with the request
|
MockMvcRequestSpecBuilder |
addHeaders(Map<String,String> headers)
Add headers to be sent with the request as Map.
|
MockMvcRequestSpecBuilder |
addMockMvcRequestSpecification(MockMvcRequestSpecification specification)
Merge this builder with settings from another specification.
|
MockMvcRequestSpecBuilder |
addMultiPart(File file)
Specify a file to upload to the server using multi-part form data uploading.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
File file)
Specify a file to upload to the server using multi-part form data uploading with a specific
control name.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
File file,
String mimeType)
Specify a file to upload to the server using multi-part form data uploading with a specific
control name and content-type.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
String contentBody)
Specify a string to send to the server using multi-part form data.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
String fileName,
byte[] bytes)
Specify a byte-array to upload to the server using multi-part form data.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
String fileName,
byte[] bytes,
String mimeType)
Specify a byte-array to upload to the server using multi-part form data.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
String fileName,
InputStream stream)
Specify an inputstream to upload to the server using multi-part form data.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
String fileName,
InputStream stream,
String mimeType)
Specify an inputstream to upload to the server using multi-part form data.
|
MockMvcRequestSpecBuilder |
addMultiPart(String controlName,
String contentBody,
String mimeType)
Specify a string to send to the server using multi-part form data with a specific mime-type.
|
MockMvcRequestSpecBuilder |
addParam(String parameterName,
Collection<?> parameterValues) |
MockMvcRequestSpecBuilder |
addParam(String parameterName,
Object... parameterValues) |
MockMvcRequestSpecBuilder |
addParams(Map<String,?> parametersMap) |
MockMvcRequestSpecBuilder |
addPathParam(String parameterName,
Object parameterValue)
Specify a path parameter.
|
MockMvcRequestSpecBuilder |
addPathParams(Map<String,Object> parameterNameValuePairs)
Specify multiple path parameter name-value pairs.
|
MockMvcRequestSpecBuilder |
addPathParams(String firstParameterName,
Object firstParameterValue,
Object... parameterNameValuePairs)
Specify multiple path parameter name-value pairs.
|
MockMvcRequestSpecBuilder |
addQueryParam(String parameterName,
Collection<?> parameterValues) |
MockMvcRequestSpecBuilder |
addQueryParam(String parameterName,
Object... parameterValues) |
MockMvcRequestSpecBuilder |
addQueryParams(Map<String,?> parametersMap) |
MockMvcRequestSpecBuilder |
addResultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler,
org.springframework.test.web.servlet.ResultHandler... additionalResultHandlers)
Add a result handler
|
MockMvcRequestSpecBuilder |
and()
Returns the same MockMvcRequestSpecBuilder instance for syntactic sugar.
|
MockMvcRequestSpecification |
build()
Build the request specification.
|
MockMvcRequestSpecBuilder |
log(io.restassured.filter.log.LogDetail logDetail)
Enabled logging with the specified log detail.
|
MockMvcRequestSpecBuilder |
setAuth(MockMvcAuthenticationScheme auth)
If you need to specify some credentials when performing a request.
|
MockMvcRequestSpecBuilder |
setBasePath(String basePath)
Set the basePath property of the MockMvcRequestSpecBuilder instead of using static field RestAssuredMockMvc.basePath.
|
MockMvcRequestSpecBuilder |
setBody(byte[] body)
Specify a byte array request body to be sent with the request.
|
MockMvcRequestSpecBuilder |
setBody(Object object)
Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request.
|
MockMvcRequestSpecBuilder |
setBody(Object object,
io.restassured.mapper.ObjectMapper mapper)
Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request using a specific object mapper.
|
MockMvcRequestSpecBuilder |
setBody(Object object,
io.restassured.mapper.ObjectMapperType mapperType)
Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request using a specific object mapper type.
|
MockMvcRequestSpecBuilder |
setBody(String body)
Specify a String request body (such as e.g.
|
MockMvcRequestSpecBuilder |
setConfig(RestAssuredMockMvcConfig config)
Define a configuration for redirection settings and http client parameters.
|
MockMvcRequestSpecBuilder |
setContentType(io.restassured.http.ContentType contentType)
Specify the content type of the request.
|
MockMvcRequestSpecBuilder |
setContentType(String contentType)
Specify the content type of the request as string.
|
MockMvcRequestSpecBuilder |
setMockHttpServletRequestBuilderInterceptor(MockHttpServletRequestBuilderInterceptor interceptor)
The mock mvc instance to use.
|
MockMvcRequestSpecBuilder |
setMockMvc(org.springframework.test.web.servlet.MockMvc mockMvc)
The mock mvc instance to use.
|
MockMvcRequestSpecBuilder |
setPostProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessors)
Set the post processors for this request.
|
MockMvcRequestSpecBuilder |
setSessionId(String sessionIdValue)
Set the session id for this request.
|
MockMvcRequestSpecBuilder |
setSessionId(String sessionIdName,
String sessionIdValue)
Set the session id name and value for this request.
|
MockMvcRequestSpecBuilder |
setStandaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder)
Initialize with a MockMvcBuilder that will be used to create the
MockMvc instance. |
MockMvcRequestSpecBuilder |
setStandaloneSetup(Object... controllers)
The standalone setup to be used by supplying a set of controllers.
|
MockMvcRequestSpecBuilder |
setWebAppContextSetup(org.springframework.web.context.WebApplicationContext context,
org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers)
Initialize with a
WebApplicationContext that will be used to create the MockMvc instance. |
public MockMvcRequestSpecBuilder setAuth(MockMvcAuthenticationScheme auth)
public MockMvcRequestSpecBuilder setPostProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessors)
postProcessors - The post processors to usepublic MockMvcRequestSpecBuilder setBody(String body)
body - The body to send.public MockMvcRequestSpecBuilder setBody(byte[] body)
body - The body to send.public MockMvcRequestSpecBuilder setBody(Object object)
object - The object to serialize and send with the requestpublic MockMvcRequestSpecBuilder setBody(Object object, io.restassured.mapper.ObjectMapper mapper)
Note that setBody(Object, ObjectMapper)
are the same except for the syntactic difference.
object - The object to serialize and send with the requestmapper - The object mapperpublic MockMvcRequestSpecBuilder setBody(Object object, io.restassured.mapper.ObjectMapperType mapperType)
Example of use:
Message message = new Message();
message.setMessage("My beautiful message");
given().
body(message, ObjectMapper.GSON).
expect().
content(equalTo("Response to a beautiful message")).
when().
post("/beautiful-message");
Note that setBody(Object, ObjectMapperType)
are the same except for the syntactic difference.
object - The object to serialize and send with the requestmapperType - The object mapper type to be usedpublic MockMvcRequestSpecBuilder addCookies(Map<String,?> cookies)
cookies - The Map containing the cookie names and their values to set in the request.public MockMvcRequestSpecBuilder addCookie(io.restassured.http.Cookie cookie)
cookie - The cookie to add.public MockMvcRequestSpecBuilder addCookie(String key, Object value, Object... cookieNameValuePairs)
key - The cookie keyvalue - The cookie valuecookieNameValuePairs - Additional cookies values. This will actually create two cookies with the same name but with different values.public MockMvcRequestSpecBuilder addParams(Map<String,?> parametersMap)
parametersMap - The Map containing the parameter names and their values to send with the request.public MockMvcRequestSpecBuilder addParam(String parameterName, Object... parameterValues)
parameterValues - Zero to many parameter values for this parameter name.public MockMvcRequestSpecBuilder addParam(String parameterName, Collection<?> parameterValues)
parameterName - The parameter keyparameterValues - The parameter valuespublic MockMvcRequestSpecBuilder addQueryParam(String parameterName, Collection<?> parameterValues)
parameterName - The parameter keyparameterValues - The parameter valuesaddQueryParam(String, Object...)public MockMvcRequestSpecBuilder addQueryParams(Map<String,?> parametersMap)
parametersMap - The Map containing the parameter names and their values to send with the request.public MockMvcRequestSpecBuilder addQueryParam(String parameterName, Object... parameterValues)
parameterName - The parameter keyparameterValues - Zero to many parameter values for this parameter name.public MockMvcRequestSpecBuilder addPathParam(String parameterName, Object parameterValue)
expect().statusCode(200).when().get("/item/"+myItem.getItemNumber()+"/buy/"+2);
you can write:
given().
pathParam("itemNumber", myItem.getItemNumber()).
pathParam("amount", 2).
expect().
statusCode(200).
when().
get("/item/{itemNumber}/buy/{amount}");
which improves readability and allows the path to be reusable in many tests. Another alternative is to use:
expect().statusCode(200).when().get("/item/{itemNumber}/buy/{amount}", myItem.getItemNumber(), 2);
parameterName - The parameter keyparameterValue - The parameter valuepublic MockMvcRequestSpecBuilder addPathParams(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs)
expect().statusCode(200).when().get("/item/"+myItem.getItemNumber()+"/buy/"+2);
you can write:
given().
pathParam("itemNumber", myItem.getItemNumber(), "amount", 2).
expect().
statusCode(200).
when().
get("/item/{itemNumber}/buy/{amount}");
which improves readability and allows the path to be reusable in many tests. Another alternative is to use:
expect().statusCode(200).when().get("/item/{itemNumber}/buy/{amount}", myItem.getItemNumber(), 2);
firstParameterName - The name of the first parameterfirstParameterValue - The value of the first parameterparameterNameValuePairs - Additional parameters in name-value pairs.public MockMvcRequestSpecBuilder addPathParams(Map<String,Object> parameterNameValuePairs)
expect().statusCode(200).when().get("/item/"+myItem.getItemNumber()+"/buy/"+2);
you can write:
Map<String,Object> pathParams = new HashMap<String,Object>();
pathParams.add("itemNumber",myItem.getItemNumber());
pathParams.add("amount",2);
given().
pathParameters(pathParams).
expect().
statusCode(200).
when().
get("/item/{itemNumber}/buy/{amount}");
which improves readability and allows the path to be reusable in many tests. Another alternative is to use:
expect().statusCode(200).when().get("/item/{itemNumber}/buy/{amount}", myItem.getItemNumber(), 2);
parameterNameValuePairs - A map containing the path parameters.public MockMvcRequestSpecBuilder addFormParam(String parameterName, Collection<?> parameterValues)
parameterName - The parameter keyparameterValues - The parameter valuesaddFormParam(String, Object...)public MockMvcRequestSpecBuilder addFormParams(Map<String,?> parametersMap)
parametersMap - The Map containing the parameter names and their values to send with the request.public MockMvcRequestSpecBuilder addFormParam(String parameterName, Object... parameterValues)
parameterName - The parameter nameparameterValues - Zero to many parameter values for this parameter name.addFormParam(String, Object...)public MockMvcRequestSpecBuilder addAttribute(String attributeName, Object attributeValue)
attributeName - The attribute nameattributeValue - The attribute valuepublic MockMvcRequestSpecBuilder addAttributes(Map<String,?> attributesMap)
attributesMap - The Map containing the request attribute names and their valuespublic MockMvcRequestSpecBuilder addHeaders(Map<String,String> headers)
headers - The Map containing the header names and their values to send with the request.public MockMvcRequestSpecBuilder addHeader(String headerName, String headerValue)
headerName - The header nameheaderValue - The header valuepublic MockMvcRequestSpecBuilder addHeader(io.restassured.http.Header header)
header - The headerpublic MockMvcRequestSpecBuilder setContentType(io.restassured.http.ContentType contentType)
contentType - The content type of the requestContentTypepublic MockMvcRequestSpecBuilder setContentType(String contentType)
contentType - The content type of the requestpublic MockMvcRequestSpecBuilder addMultiPart(File file)
file - The file to uploadpublic MockMvcRequestSpecBuilder addMultiPart(String controlName, File file)
file - The file to uploadcontrolName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.public MockMvcRequestSpecBuilder addMultiPart(String controlName, File file, String mimeType)
file - The file to uploadcontrolName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.mimeType - The content-typepublic MockMvcRequestSpecBuilder addMultiPart(String controlName, String fileName, byte[] bytes)
controlName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.fileName - The name of the content you're uploadingbytes - The bytes you want to sendpublic MockMvcRequestSpecBuilder addMultiPart(String controlName, String fileName, byte[] bytes, String mimeType)
controlName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.fileName - The name of the content you're uploadingbytes - The bytes you want to sendmimeType - The content-typepublic MockMvcRequestSpecBuilder addMultiPart(String controlName, String fileName, InputStream stream)
controlName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.fileName - The name of the content you're uploadingstream - The stream you want to sendpublic MockMvcRequestSpecBuilder addMultiPart(String controlName, String fileName, InputStream stream, String mimeType)
controlName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.fileName - The name of the content you're uploadingstream - The stream you want to sendmimeType - The content-typepublic MockMvcRequestSpecBuilder addMultiPart(String controlName, String contentBody)
controlName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.contentBody - The string to sendpublic MockMvcRequestSpecBuilder addMultiPart(String controlName, String contentBody, String mimeType)
controlName - Defines the control name of the body part. In HTML this is the attribute name of the input tag.contentBody - The string to sendmimeType - The mime-typepublic MockMvcRequestSpecBuilder setSessionId(String sessionIdValue)
RestAssuredMockMvc.config = newConfig().sessionConfig(new SessionConfig().sessionIdName(<sessionIdName>));
or you can use the setSessionId(String, String) method to set it for this request only.sessionIdValue - The session id value.public MockMvcRequestSpecBuilder setSessionId(String sessionIdName, String sessionIdValue)
RestAssuredMockMvc.config = newConfig().sessionConfig(new SessionConfig().sessionIdName(<sessionIdName>));
and then you can use the setSessionId(String) method to set the session id value without specifying the name for each request.sessionIdName - The session id namesessionIdValue - The session id value.public MockMvcRequestSpecBuilder addMockMvcRequestSpecification(MockMvcRequestSpecification specification)
specification - The specification to addpublic MockMvcRequestSpecBuilder setConfig(RestAssuredMockMvcConfig config)
config - The configuration to use for this request. If null no config will be used.public MockMvcRequestSpecification build()
public MockMvcRequestSpecBuilder setBasePath(String basePath)
MockMvcRequestSpecBuilder builder = new MockMvcRequestSpecBuilder();
builder.setBasePath("/something");
MockMvcRequestSpecification specs = builder.build();
given().spec(specs)
basePath - public MockMvcRequestSpecBuilder setMockMvc(org.springframework.test.web.servlet.MockMvc mockMvc)
MockMvc instances configured by other setters.*mockMvc - The mock mvc instancepublic MockMvcRequestSpecBuilder setStandaloneSetup(Object... controllers)
MockMvc instances configured by other setters.controllers - The controllers to useMockMvcRequestSpecification.standaloneSetup(Object...)public MockMvcRequestSpecBuilder setStandaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder)
MockMvc instance.
Note that this will override the any MockMvc instances configured by other setters.builder - The builder to useMockMvcRequestSpecification.standaloneSetup(MockMvcBuilder)public MockMvcRequestSpecBuilder setWebAppContextSetup(org.springframework.web.context.WebApplicationContext context, org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers)
WebApplicationContext that will be used to create the MockMvc instance.
Note that this will override the any MockMvc instances configured by other setters.context - The WebApplicationContext to usemockMvcConfigurers - MockMvcConfigurer's to be applied when creating a MockMvc instance of this WebApplicationContext (optional)MockMvcRequestSpecification.webAppContextSetup(WebApplicationContext, MockMvcConfigurer...)public MockMvcRequestSpecBuilder setMockHttpServletRequestBuilderInterceptor(MockHttpServletRequestBuilderInterceptor interceptor)
interceptor - The interceptorpublic MockMvcRequestSpecBuilder addResultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler, org.springframework.test.web.servlet.ResultHandler... additionalResultHandlers)
resultHandler - The result handlerpublic MockMvcRequestSpecBuilder log(io.restassured.filter.log.LogDetail logDetail)
LogConfig to configure the print stream and pretty printing options.logDetail - The log detail.public MockMvcRequestSpecBuilder and()
Copyright © 2010–2025. All rights reserved.