Class RestAssuredMockMvc
RestAssured. This is the starting point of the DSL.
Note that some Javadoc is copied from Spring MVC's test documentation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic MockMvcAuthenticationSchemeDefines a global authentication scheme that'll be used for all requests (if not overridden).static StringThe base path that's used by REST assured when making requests.static RestAssuredMockMvcConfigDefine a REST Assured Mock Mvc configuration.static MockMvcRequestSpecificationSpecify a default request specification that will be sent with each request.static io.restassured.specification.ResponseSpecificationSpecify a default response specification that will be sent with each request. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MockMvcAuthenticationSchemeauthentication(Object authentication) Authenticate using the supplied authentication instance (org.springframework.security.core.Authenticationfrom Spring Security).static RestAssuredMockMvcConfigconfig()static MockMvcResponsedelete()Perform a DELETE request to the statically configured base path.static MockMvcResponsePerform a DELETE request to apath.static MockMvcResponsePerform a DELETE request to apath.static MockMvcResponsePerform a DELETE request to auri.static MockMvcResponsePerform a DELETE request to aurl.static voidEnable logging of both the request and the response if REST Assureds test validation fails with log detail equal toLogDetail.ALL.static voidenableLoggingOfRequestAndResponseIfValidationFails(io.restassured.filter.log.LogDetail logDetail) Enable logging of both the request and the response if REST Assureds test validation fails with the specified log detail.static MockMvcResponseget()Perform a GET request to the statically configured base path.static MockMvcResponsePerform a GET request to apath.static MockMvcResponsePerform a GET request to apath.static MockMvcResponsePerform a GET request to auri.static MockMvcResponsePerform a GET request to aurl.static MockMvcRequestSpecificationgiven()This is usually the entry-point of the API if you need to specify parameters or a body in the request.static MockMvcResponsehead()Perform a HEAD request to the statically configured base path.static MockMvcResponsePerform a HEAD request to apath.static MockMvcResponsePerform a HEAD request to apath.static MockMvcResponsePerform a HEAD request to auri.static MockMvcResponsePerform a HEAD request to aurl.static voidmockMvc(org.springframework.test.web.servlet.MockMvc mockMvc) Set aMockMvcinstance that REST Assured will use when making requests unless overwritten by aMockMvcRequestSpecification.static MockMvcResponseoptions()Perform a OPTIONS request to the statically configured base path.static MockMvcResponsePerform a OPTIONS request to apath.static MockMvcResponsePerform a OPTIONS request to apath.static MockMvcResponsePerform a OPTIONS request to auri.static MockMvcResponsePerform a OPTIONS request to aurl.static MockMvcResponsepatch()Perform a PATCH request to the statically configured base path.static MockMvcResponsePerform a PATCH request to apath.static MockMvcResponsePerform a PATCH request to apath.static MockMvcResponsePerform a PATCH request to auri.static MockMvcResponsePerform a PATCH request to aurl.static MockMvcResponsepost()Perform a POST request to the statically configured base path.static MockMvcResponsePerform a POST request to apath.static MockMvcResponsePerform a POST request to apath.static MockMvcResponsePerform a POST request to auri.static MockMvcResponsePerform a POST request to aurl.static List<org.springframework.test.web.servlet.request.RequestPostProcessor>static voidpostProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalPostProcessors) Assign one or moreResultHandlerthat'll be executes after a request has been made.static MockMvcAuthenticationSchemeAuthenticate using the given principal.static MockMvcAuthenticationSchemeAuthenticate using the given principal.static MockMvcAuthenticationSchemeprincipalWithCredentials(Object principal, Object credentials, String... authorities) Authenticate using the given principal and credentials.static MockMvcResponseput()Perform a PUT request to the statically configured base path.static MockMvcResponsePerform a PUT request to apath.static MockMvcResponsePerform a PUT request to auri.static MockMvcResponsePerform a PUT request to aurl.static MockMvcResponserequest(io.restassured.http.Method method) Perform a request to the pre-configured path (by defaulthttp://localhost:8080).static MockMvcResponsePerform a HTTP request to apath.static MockMvcResponsePerform a request to auri.static MockMvcResponsePerform a request to aurl.static MockMvcResponsePerform a custom HTTP request to the pre-configured path (by defaulthttp://localhost:8080).static MockMvcResponsePerform a custom HTTP request to apath.static MockMvcResponsePerform a custom HTTP request to auri.static MockMvcResponsePerform a custom HTTP request to aurl.static voidreset()Reset all static configurations to their default values.static List<org.springframework.test.web.servlet.ResultHandler>static voidresultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler, org.springframework.test.web.servlet.ResultHandler... resultHandlers) Assign one or moreResultHandlerthat'll be executes after a request has been made.static voidstandaloneSetup(Object... controllersOrMockMvcConfigurers) Build aMockMvcby registering one or more@Controller's instances and configuring Spring MVC infrastructure programmatically.static voidstandaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder) Build aMockMvcby using a providedAbstractMockMvcBuilderfor configuring Spring MVC infrastructure programmatically.static voidwebAppContextSetup(org.springframework.web.context.WebApplicationContext context, org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers) Build aMockMvcusing the given, fully initialized, i.e.static MockMvcRequestSenderwhen()This is usually the entry-point of the API if you need to specify parameters or a body in the request.static MockMvcRequestSpecificationwith()This is usually the entry-point of the API if you need to specify parameters or a body in the request.static MockMvcAuthenticationSchemewith(org.springframework.test.web.servlet.request.RequestPostProcessor requestPostProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalRequestPostProcessor) Authenticate using aRequestPostProcessor.
-
Field Details
-
config
Define a REST Assured Mock Mvc configuration. E.g.given().config(newConfig().logConfig(new LogConfig(captor, true))). ..
newConfig()can be statically imported fromRestAssuredMockMvcConfig. -
requestSpecification
Specify a default request specification that will be sent with each request. E,g.RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().addParam("parameter1", "value1").build();means that for each request by Rest Assured "parameter1" will be equal to "value1". -
responseSpecification
public static io.restassured.specification.ResponseSpecification responseSpecificationSpecify a default response specification that will be sent with each request. E,g.RestAssuredMockMvc.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).build();
means that for each response Rest Assured will assert that the status code is equal to 200. -
basePath
The base path that's used by REST assured when making requests. The base path is prepended to the request path. Default value is/. -
authentication
Defines a global authentication scheme that'll be used for all requests (if not overridden). Usage example:RestAssured.authentication = principal(myPrincipal);
-
-
Constructor Details
-
RestAssuredMockMvc
public RestAssuredMockMvc()
-
-
Method Details
-
mockMvc
public static void mockMvc(org.springframework.test.web.servlet.MockMvc mockMvc) Set aMockMvcinstance that REST Assured will use when making requests unless overwritten by aMockMvcRequestSpecification.- Parameters:
mockMvc- The MockMvc instance to use.
-
given
This is usually the entry-point of the API if you need to specify parameters or a body in the request. For example:given(). param("x", "y"). when(). get("/something"). then(). statusCode(200). body("x.y", notNullValue());Note that this method is the same aswith()but with another syntax.- Returns:
- A
MockMvcRequestSpecification.
-
with
This is usually the entry-point of the API if you need to specify parameters or a body in the request. For example:given(). param("x", "y"). when(). get("/something"). then(). statusCode(200). body("x.y", notNullValue());Note that this method is the same asgiven()but with another syntax.- Returns:
- A
MockMvcRequestSpecification.
-
when
This is usually the entry-point of the API if you need to specify parameters or a body in the request. For example:when(). get("/x"). then(). body("x.y.z1", equalTo("Z1")). body("x.y.z2", equalTo("Z2"));Note that if you need to add parameters, headers, cookies or other request properties use the
given()method.- Returns:
- A request sender interface that let's you call resources on the server
-
standaloneSetup
Build aMockMvcby registering one or more@Controller's instances and configuring Spring MVC infrastructure programmatically. This allows full control over the instantiation and initialization of controllers, and their dependencies, similar to plain unit tests while also making it possible to test one controller at a time.When this option is used, the minimum infrastructure required by the
DispatcherServletto serve requests with annotated controllers is automatically created, and can be customized, resulting in configuration that is equivalent to what the MVC Java configuration provides except using builder style methods.If the Spring MVC configuration of an application is relatively straight-forward, for example when using the MVC namespace or the MVC Java config, then using this builder might be a good option for testing a majority of controllers. A much smaller number of tests can be used to focus on testing and verifying the actual Spring MVC configuration.
- Parameters:
controllersOrMockMvcConfigurers- one or more@Controller's to test as well as @{link MockMvcConfigurer}'s to apply
-
standaloneSetup
public static void standaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder) Build aMockMvcby using a providedAbstractMockMvcBuilderfor configuring Spring MVC infrastructure programmatically. This allows full control over the instantiation and initialization of controllers, and their dependencies, similar to plain unit tests while also making it possible to test one controller at a time.If the Spring MVC configuration of an application is relatively straight-forward, for example when using the MVC namespace or the MVC Java config, then using this builder might be a good option for testing a majority of controllers. A much smaller number of tests can be used to focus on testing and verifying the actual Spring MVC configuration.
- Parameters:
builder-AbstractMockMvcBuilderto build the MVC mock
-
webAppContextSetup
public static void webAppContextSetup(org.springframework.web.context.WebApplicationContext context, org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers) Build aMockMvcusing the given, fully initialized, i.e. refreshed,WebApplicationContextand assign it to REST Assured. TheDispatcherServletwill use the context to discover Spring MVC infrastructure and application controllers in it. The context must have been configured with aServletContext.- Parameters:
context- The web application context to usemockMvcConfigurers-MockMvcConfigurer's to be applied when creating aMockMvcinstance of this WebApplicationContext (optional)
-
resultHandlers
public static void resultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler, org.springframework.test.web.servlet.ResultHandler... resultHandlers) Assign one or moreResultHandlerthat'll be executes after a request has been made.- Parameters:
resultHandler- The result handlerresultHandlers- Additional result handlers (optional)
-
resultHandlers
- Returns:
- The defined list of result handlers
-
postProcessors
public static void postProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalPostProcessors) Assign one or moreResultHandlerthat'll be executes after a request has been made.Note that it's recommended to use
with(RequestPostProcessor, RequestPostProcessor...)instead of this method when setting authentication/authorization based RequestPostProcessors.- Parameters:
postProcessor- a post-processor to addadditionalPostProcessors- Additional post-processors to add- See Also:
-
postProcessors
public static List<org.springframework.test.web.servlet.request.RequestPostProcessor> postProcessors()- Returns:
- The defined list of request post processors
-
reset
public static void reset()Reset all static configurations to their default values. -
get
Perform a GET request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can doget("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the GET request.
-
get
Perform a GET request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters.- Returns:
- The response of the GET request.
-
post
Perform a POST request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can dopost("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
post
Perform a POST request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters.- Returns:
- The response of the request.
-
put
Perform a PUT request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can doput("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
delete
Perform a DELETE request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can dodelete("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
delete
Perform a DELETE request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters.- Returns:
- The response of the request.
-
head
Perform a HEAD request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can dohead("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
head
Perform a HEAD request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters.- Returns:
- The response of the request.
-
patch
Perform a PATCH request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can dohead("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
patch
Perform a PATCH request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters.- Returns:
- The response of the request.
-
options
Perform a OPTIONS request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can dohead("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
options
Perform a OPTIONS request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
path- The path to send the request to.pathParams- The path parameters.- Returns:
- The response of the request.
-
get
Perform a GET request to auri.- Parameters:
uri- The uri to send the request to.- Returns:
- The response of the GET request.
-
post
Perform a POST request to auri.- Parameters:
uri- The uri to send the request to.- Returns:
- The response of the request.
-
put
Perform a PUT request to auri.- Parameters:
uri- The uri to send the request to.- Returns:
- The response of the request.
-
delete
Perform a DELETE request to auri.- Parameters:
uri- The uri to send the request to.- Returns:
- The response of the request.
-
head
Perform a HEAD request to auri.- Parameters:
uri- The uri to send the request to.- Returns:
- The response of the request.
-
patch
Perform a PATCH request to auri.- Parameters:
uri- The uri to send the request to.- Returns:
- The response of the request.
-
options
Perform a OPTIONS request to auri.- Parameters:
uri- The uri to send the request to.- Returns:
- The response of the request.
-
get
Perform a GET request to aurl.- Parameters:
url- The url to send the request to.- Returns:
- The response of the GET request.
-
post
Perform a POST request to aurl.- Parameters:
url- The url to send the request to.- Returns:
- The response of the request.
-
put
Perform a PUT request to aurl.- Parameters:
url- The url to send the request to.- Returns:
- The response of the request.
-
delete
Perform a DELETE request to aurl.- Parameters:
url- The url to send the request to.- Returns:
- The response of the request.
-
head
Perform a HEAD request to aurl.- Parameters:
url- The url to send the request to.- Returns:
- The response of the request.
-
patch
Perform a PATCH request to aurl.- Parameters:
url- The url to send the request to.- Returns:
- The response of the request.
-
options
Perform a OPTIONS request to aurl.- Parameters:
url- The url to send the request to.- Returns:
- The response of the request.
-
get
Perform a GET request to the statically configured base path.- Returns:
- The response of the GET request.
-
post
Perform a POST request to the statically configured base path.- Returns:
- The response of the request.
-
put
Perform a PUT request to the statically configured base path.- Returns:
- The response of the request.
-
delete
Perform a DELETE request to the statically configured base path.- Returns:
- The response of the request.
-
head
Perform a HEAD request to the statically configured base path.- Returns:
- The response of the request.
-
patch
Perform a PATCH request to the statically configured base path.- Returns:
- The response of the request.
-
options
Perform a OPTIONS request to the statically configured base path.- Returns:
- The response of the request.
-
request
Perform a request to the pre-configured path (by defaulthttp://localhost:8080).- Parameters:
method- The HTTP method to use- Returns:
- The response of the request.
-
request
Perform a custom HTTP request to the pre-configured path (by defaulthttp://localhost:8080).- Parameters:
method- The HTTP method to use- Returns:
- The response of the request.
-
request
public static MockMvcResponse request(io.restassured.http.Method method, String path, Object... pathParams) Perform a HTTP request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
method- The HTTP method to usepath- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can dorequest(Method.TRACE,"/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
request
Perform a custom HTTP request to apath. Normally the path doesn't have to be fully-qualified e.g. you don't need to specify the path as http://localhost:8080/path. In this case it's enough to use /path.- Parameters:
method- The HTTP method to usepath- The path to send the request to.pathParams- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can dorequest("method","/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);.- Returns:
- The response of the request.
-
request
Perform a request to auri.- Parameters:
method- The HTTP method to useuri- The uri to send the request to.- Returns:
- The response of the GET request.
-
request
Perform a request to aurl.- Parameters:
method- The HTTP method to useurl- The url to send the request to.- Returns:
- The response of the GET request.
-
request
Perform a custom HTTP request to auri.- Parameters:
method- The HTTP method to useuri- The uri to send the request to.- Returns:
- The response of the GET request.
-
request
Perform a custom HTTP request to aurl.- Parameters:
method- The HTTP method to useurl- The url to send the request to.- Returns:
- The response of the GET request.
-
principal
Authenticate using the given principal. Used as:RestAssured.authentication = principal(myPrincipal);
or in aMockMvcRequestSpecBuilder:MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principal(myPrincipal)). ..
- Parameters:
principal- The principal to use.- Returns:
- A
MockMvcAuthenticationSchemeinstance. - See Also:
-
principal
Authenticate using the given principal. Used as:RestAssured.authentication = principal(myPrincipal);
or in aMockMvcRequestSpecBuilder:MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principal(myPrincipal)). ..
- Parameters:
principal- The principal to use.- Returns:
- A
MockMvcAuthenticationSchemeinstance. - See Also:
-
principalWithCredentials
public static MockMvcAuthenticationScheme principalWithCredentials(Object principal, Object credentials, String... authorities) Authenticate using the given principal and credentials. Used as:RestAssured.authentication = principalWithCredentials(myPrincipal, myCredentials);
or in aMockMvcRequestSpecBuilder:MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principalWithCredentials(myPrincipal, myCredentials)). ..
- Parameters:
principal- The principal to use.credentials- The credentials to useauthorities- Optional list of authorities- Returns:
- A
MockMvcAuthenticationSchemeinstance. - See Also:
-
authentication
Authenticate using the supplied authentication instance (org.springframework.security.core.Authenticationfrom Spring Security). Used as:RestAssured.authentication = authentication(myAuth);
or in aMockMvcRequestSpecBuilder:MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(authentication(myAuth)). ..
- Parameters:
authentication- The authentication instance to use.- Returns:
- A
MockMvcAuthenticationSchemeinstance. - See Also:
-
with
public static MockMvcAuthenticationScheme with(org.springframework.test.web.servlet.request.RequestPostProcessor requestPostProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalRequestPostProcessor) Authenticate using aRequestPostProcessor. This is mainly useful when you have added thespring-security-testartifact to classpath. This allows you to do for example:RestAssured.authentication = with(user("username").password("password"));whereuseris statically imported fromorg.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.- Parameters:
requestPostProcessor- The first request post processor to be used for authenticationadditionalRequestPostProcessor- Additional request post processors to be used for authentication- Returns:
- A
MockMvcAuthenticationSchemeinstance.
-
enableLoggingOfRequestAndResponseIfValidationFails
public static void enableLoggingOfRequestAndResponseIfValidationFails()Enable logging of both the request and the response if REST Assureds test validation fails with log detail equal toLogDetail.ALL.This is just a shortcut for:
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails());
-
enableLoggingOfRequestAndResponseIfValidationFails
public static void enableLoggingOfRequestAndResponseIfValidationFails(io.restassured.filter.log.LogDetail logDetail) Enable logging of both the request and the response if REST Assureds test validation fails with the specified log detail.This is just a shortcut for:
RestAssured.config = new RestAssuredMockMvcConfig().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail));
- Parameters:
logDetail- The log detail to show in the log
-
config
- Returns:
- The assigned config or a new config is no config is assigned
-