Class MockMvcRequestSpecBuilder
java.lang.Object
io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder
You can use the builder to construct a request specification. The specification can be used as e.g.
MockMvcRequestSpecification requestSpec = new MockMvcRequestSpecBuilder().addParameter("parameter1", "value1").build();
given().
spec(requestSpec).
when().
get("/something").
then().
body("x.y.z", equalTo("something")).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddAttribute(String attributeName, Object attributeValue) Add request attributeaddAttributes(Map<String, ?> attributesMap) Add request attributesaddCookie(io.restassured.http.Cookie cookie) Add a detailed cookieAdd a cookie to be sent with the request.addCookies(Map<String, ?> cookies) Add cookies to be sent with the request as Map e.g:addFormParam(String parameterName, Object... parameterValues) addFormParam(String parameterName, Collection<?> parameterValues) addFormParams(Map<String, ?> parametersMap) addHeader(io.restassured.http.Header header) Add a header to be sent with the request.Add a header to be sent with the requestaddHeaders(Map<String, String> headers) Add headers to be sent with the request as Map.addMockMvcRequestSpecification(MockMvcRequestSpecification specification) Merge this builder with settings from another specification.addMultiPart(File file) Specify a file to upload to the server using multi-part form data uploading.addMultiPart(String controlName, File file) Specify a file to upload to the server using multi-part form data uploading with a specific control name.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.addMultiPart(String controlName, String contentBody) Specify a string to send to the server using multi-part form data.addMultiPart(String controlName, String fileName, byte[] bytes) Specify a byte-array to upload to the server using multi-part form data.addMultiPart(String controlName, String fileName, byte[] bytes, String mimeType) Specify a byte-array to upload to the server using multi-part form data.addMultiPart(String controlName, String fileName, InputStream stream) Specify an inputstream to upload to the server using multi-part form data.addMultiPart(String controlName, String fileName, InputStream stream, String mimeType) Specify an inputstream to upload to the server using multi-part form data.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.addParam(String parameterName, Collection<?> parameterValues) addPathParam(String parameterName, Object parameterValue) Specify a path parameter.addPathParams(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs) Specify multiple path parameter name-value pairs.addPathParams(Map<String, Object> parameterNameValuePairs) Specify multiple path parameter name-value pairs.addQueryParam(String parameterName, Object... parameterValues) addQueryParam(String parameterName, Collection<?> parameterValues) addQueryParams(Map<String, ?> parametersMap) addResultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler, org.springframework.test.web.servlet.ResultHandler... additionalResultHandlers) Add a result handlerand()Returns the same MockMvcRequestSpecBuilder instance for syntactic sugar.build()Build the request specification.log(io.restassured.filter.log.LogDetail logDetail) Enabled logging with the specified log detail.If you need to specify some credentials when performing a request.setBasePath(String basePath) Set the basePath property of the MockMvcRequestSpecBuilder instead of using static field RestAssuredMockMvc.basePath.setBody(byte[] body) Specify a byte array request body to be sent with the request.Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request.Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request using a specific object 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 type.Specify a String request body (such as e.g.setConfig(RestAssuredMockMvcConfig config) Define a configuration for redirection settings and http client parameters.setContentType(io.restassured.http.ContentType contentType) Specify the content type of the request.setContentType(String contentType) Specify the content type of the request as string.The mock mvc instance to use.setMockMvc(org.springframework.test.web.servlet.MockMvc mockMvc) The mock mvc instance to use.setPostProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessors) Set the post processors for this request.setSessionId(String sessionIdValue) Set the session id for this request.setSessionId(String sessionIdName, String sessionIdValue) Set the session id name and value for this request.setStandaloneSetup(Object... controllers) The standalone setup to be used by supplying a set of controllers.setStandaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder) Initialize with a MockMvcBuilder that will be used to create theMockMvcinstance.setWebAppContextSetup(org.springframework.web.context.WebApplicationContext context, org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers) Initialize with aWebApplicationContextthat will be used to create theMockMvcinstance.
-
Constructor Details
-
MockMvcRequestSpecBuilder
public MockMvcRequestSpecBuilder()
-
-
Method Details
-
setAuth
If you need to specify some credentials when performing a request.- Returns:
- The request specification builder
-
setPostProcessors
public MockMvcRequestSpecBuilder setPostProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessors) Set the post processors for this request.- Parameters:
postProcessors- The post processors to use- Returns:
- The request specification builder
-
setBody
Specify a String request body (such as e.g. JSON or XML) to be sent with the request. This works for the POST, PUT and PATCH methods only. Trying to do this for the other http methods will cause an exception to be thrown.- Parameters:
body- The body to send.- Returns:
- The request specification builder
-
setBody
Specify a byte array request body to be sent with the request. This only works for the POST http method. Trying to do this for the other http methods will cause an exception to be thrown.- Parameters:
body- The body to send.- Returns:
- The request specification builder
-
setBody
Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request. If the object is a primitive or Number the object will be converted to a String and put in the request body. This works for the POST, PUT and PATCH methods only. Trying to do this for the other http methods will cause an exception to be thrown.- Parameters:
object- The object to serialize and send with the request- Returns:
- The request specification
-
setBody
Specify an Object request content that will automatically be serialized to JSON or XML and sent with the request using a specific object mapper. This works for the POST, PATCH and PUT methods only. Trying to do this for the other http methods will cause an exception to be thrown.Note that
setBody(Object, ObjectMapper)are the same except for the syntactic difference.- Parameters:
object- The object to serialize and send with the requestmapper- The object mapper- Returns:
- The request specification
-
setBody
public 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. This works for the POST, PATCH and PUT methods only. Trying to do this for the other http methods will cause an exception to be thrown.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 thatsetBody(Object, ObjectMapperType)are the same except for the syntactic difference.- Parameters:
object- The object to serialize and send with the requestmapperType- The object mapper type to be used- Returns:
- The request specification
-
addCookies
Add cookies to be sent with the request as Map e.g:- Parameters:
cookies- The Map containing the cookie names and their values to set in the request.- Returns:
- The request specification builder
-
addCookie
Add a detailed cookie- Parameters:
cookie- The cookie to add.- Returns:
- The request specification builder
-
addCookie
public MockMvcRequestSpecBuilder addCookie(String key, Object value, Object... cookieNameValuePairs) Add a cookie to be sent with the request.- Parameters:
key- The cookie keyvalue- The cookie valuecookieNameValuePairs- Additional cookies values. This will actually create two cookies with the same name but with different values.- Returns:
- The request specification builder
-
addParams
- Parameters:
parametersMap- The Map containing the parameter names and their values to send with the request.- Returns:
- The request specification builder
-
addParam
- Parameters:
parameterValues- Zero to many parameter values for this parameter name.- Returns:
- The request specification builder
-
addParam
- Parameters:
parameterName- The parameter keyparameterValues- The parameter values- Returns:
- The request specification builder
-
addQueryParam
- Parameters:
parameterName- The parameter keyparameterValues- The parameter values- Returns:
- The request specification builder
- See Also:
-
addQueryParams
- Parameters:
parametersMap- The Map containing the parameter names and their values to send with the request.- Returns:
- The request specification builder
-
addQueryParam
- Parameters:
parameterName- The parameter keyparameterValues- Zero to many parameter values for this parameter name.- Returns:
- The request specification builder
-
addPathParam
Specify a path parameter. Path parameters are used to improve readability of the request path. E.g. instead of writing: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);- Parameters:
parameterName- The parameter keyparameterValue- The parameter value- Returns:
- The request specification
-
addPathParams
public MockMvcRequestSpecBuilder addPathParams(String firstParameterName, Object firstParameterValue, Object... parameterNameValuePairs) Specify multiple path parameter name-value pairs. Path parameters are used to improve readability of the request path. E.g. instead of writing: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);- Parameters:
firstParameterName- The name of the first parameterfirstParameterValue- The value of the first parameterparameterNameValuePairs- Additional parameters in name-value pairs.- Returns:
- The request specification
-
addPathParams
Specify multiple path parameter name-value pairs. Path parameters are used to improve readability of the request path. E.g. instead of writing: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);- Parameters:
parameterNameValuePairs- A map containing the path parameters.- Returns:
- The request specification
-
addFormParam
- Parameters:
parameterName- The parameter keyparameterValues- The parameter values- Returns:
- The request specification builder
- See Also:
-
addFormParams
- Parameters:
parametersMap- The Map containing the parameter names and their values to send with the request.- Returns:
- The request specification builder
-
addFormParam
- Parameters:
parameterName- The parameter nameparameterValues- Zero to many parameter values for this parameter name.- Returns:
- The request specification builder
- See Also:
-
addAttribute
Add request attribute- Parameters:
attributeName- The attribute nameattributeValue- The attribute value- Returns:
- The request specification builder
-
addAttributes
Add request attributes- Parameters:
attributesMap- The Map containing the request attribute names and their values- Returns:
- The request specification builder
-
addHeaders
Add headers to be sent with the request as Map.- Parameters:
headers- The Map containing the header names and their values to send with the request.- Returns:
- The request specification builder
-
addHeader
Add a header to be sent with the request- Parameters:
headerName- The header nameheaderValue- The header value- Returns:
- The request specification builder
-
addHeader
Add a header to be sent with the request.- Parameters:
header- The header- Returns:
- The request specification builder
-
setContentType
Specify the content type of the request.- Parameters:
contentType- The content type of the request- Returns:
- The request specification builder
- See Also:
-
ContentType
-
setContentType
Specify the content type of the request as string.- Parameters:
contentType- The content type of the request- Returns:
- The request specification builder
-
addMultiPart
Specify a file to upload to the server using multi-part form data uploading. It will assume that the control name is file and the content-type is application/octet-stream. If this is not what you want please use an overloaded method.- Parameters:
file- The file to upload- Returns:
- The request specification
-
addMultiPart
Specify a file to upload to the server using multi-part form data uploading with a specific control name. It will use the content-type application/octet-stream. If this is not what you want please use an overloaded method.- Parameters:
file- The file to uploadcontrolName- Defines the control name of the body part. In HTML this is the attribute name of the input tag.- Returns:
- The request specification
-
addMultiPart
Specify a file to upload to the server using multi-part form data uploading with a specific control name and content-type.- Parameters:
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-type- Returns:
- The request specification
-
addMultiPart
Specify a byte-array to upload to the server using multi-part form data. It will use the content-type application/octet-stream. If this is not what you want please use an overloaded method.- Parameters:
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 send- Returns:
- The request specification
-
addMultiPart
public MockMvcRequestSpecBuilder addMultiPart(String controlName, String fileName, byte[] bytes, String mimeType) Specify a byte-array to upload to the server using multi-part form data.- Parameters:
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-type- Returns:
- The request specification
-
addMultiPart
public MockMvcRequestSpecBuilder addMultiPart(String controlName, String fileName, InputStream stream) Specify an inputstream to upload to the server using multi-part form data. It will use the content-type application/octet-stream. If this is not what you want please use an overloaded method.- Parameters:
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 send- Returns:
- The request specification
-
addMultiPart
public MockMvcRequestSpecBuilder addMultiPart(String controlName, String fileName, InputStream stream, String mimeType) Specify an inputstream to upload to the server using multi-part form data.- Parameters:
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-type- Returns:
- The request specification
-
addMultiPart
Specify a string to send to the server using multi-part form data. It will use the content-type text/plain. If this is not what you want please use an overloaded method.- Parameters:
controlName- Defines the control name of the body part. In HTML this is the attribute name of the input tag.contentBody- The string to send- Returns:
- The request specification
-
addMultiPart
public 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.- Parameters:
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-type- Returns:
- The request specification
-
setSessionId
Set the session id for this request. It will use the configured session id name from the configuration (by default this is ). You can configure the session id name by using:RestAssuredMockMvc.config = newConfig().sessionConfig(new SessionConfig().sessionIdName(<sessionIdName>));or you can use thesetSessionId(String, String)method to set it for this request only.- Parameters:
sessionIdValue- The session id value.- Returns:
- The request specification
-
setSessionId
Set the session id name and value for this request. It'll override the default session id name from the configuration (by default this is ). You can configure the default session id name by using:RestAssuredMockMvc.config = newConfig().sessionConfig(new SessionConfig().sessionIdName(<sessionIdName>));and then you can use thesetSessionId(String)method to set the session id value without specifying the name for each request.- Parameters:
sessionIdName- The session id namesessionIdValue- The session id value.- Returns:
- The request specification
-
addMockMvcRequestSpecification
public MockMvcRequestSpecBuilder addMockMvcRequestSpecification(MockMvcRequestSpecification specification) Merge this builder with settings from another specification. Note that the supplied specification can overwrite data in the current specification. The following settings are overwritten:- Content type
- Request body
- Interceptors
- Config (if defined)
- Parameters
- Cookies
- Headers
- Parameters:
specification- The specification to add- Returns:
- The request specification builder
-
setConfig
Define a configuration for redirection settings and http client parameters.- Parameters:
config- The configuration to use for this request. Ifnullno config will be used.- Returns:
- The request specification builder
-
build
Build the request specification.- Returns:
- The assembled request specification
-
setBasePath
Set the basePath property of the MockMvcRequestSpecBuilder instead of using static field RestAssuredMockMvc.basePath.MockMvcRequestSpecBuilder builder = new MockMvcRequestSpecBuilder(); builder.setBasePath("/something"); MockMvcRequestSpecification specs = builder.build(); given().spec(specs)- Parameters:
basePath-- Returns:
- MockMvcRequestSpecBuilder
-
setMockMvc
The mock mvc instance to use. Note that this will override the anyMockMvcinstances configured by other setters.*- Parameters:
mockMvc- The mock mvc instance- Returns:
- MockMvcRequestSpecBuilder
-
setStandaloneSetup
The standalone setup to be used by supplying a set of controllers. Note that this will override the anyMockMvcinstances configured by other setters.- Parameters:
controllers- The controllers to use- Returns:
- MockMvcRequestSpecBuilder
- See Also:
-
setStandaloneSetup
public MockMvcRequestSpecBuilder setStandaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder) Initialize with a MockMvcBuilder that will be used to create theMockMvcinstance. Note that this will override the anyMockMvcinstances configured by other setters.- Parameters:
builder- The builder to use- Returns:
- MockMvcRequestSpecBuilder
- See Also:
-
setWebAppContextSetup
public MockMvcRequestSpecBuilder setWebAppContextSetup(org.springframework.web.context.WebApplicationContext context, org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers) Initialize with aWebApplicationContextthat will be used to create theMockMvcinstance. Note that this will override the anyMockMvcinstances configured by other setters.- Parameters:
context- The WebApplicationContext to usemockMvcConfigurers-MockMvcConfigurer's to be applied when creating aMockMvcinstance of this WebApplicationContext (optional)- Returns:
- MockMvcRequestSpecBuilder
- See Also:
-
setMockHttpServletRequestBuilderInterceptor
public MockMvcRequestSpecBuilder setMockHttpServletRequestBuilderInterceptor(MockHttpServletRequestBuilderInterceptor interceptor) The mock mvc instance to use.- Parameters:
interceptor- The interceptor- Returns:
- MockMvcRequestSpecBuilder
-
addResultHandlers
public MockMvcRequestSpecBuilder addResultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler, org.springframework.test.web.servlet.ResultHandler... additionalResultHandlers) Add a result handler- Parameters:
resultHandler- The result handler- Returns:
- MockMvcRequestSpecBuilder
-
log
Enabled logging with the specified log detail. Set aLogConfigto configure the print stream and pretty printing options.- Parameters:
logDetail- The log detail.- Returns:
- MockMvcRequestSpecBuilder
-
and
Returns the same MockMvcRequestSpecBuilder instance for syntactic sugar.- Returns:
- MockMvcRequestSpecBuilder
-