public class RestAssuredMockMvc extends Object
RestAssured
. This is the starting point of the DSL.
Note that some Javadoc is copied from Spring MVC's test documentation.
Modifier and Type | Field and Description |
---|---|
static MockMvcAuthenticationScheme |
authentication
Defines a global authentication scheme that'll be used for all requests (if not overridden).
|
static String |
basePath
The base path that's used by REST assured when making requests.
|
static RestAssuredMockMvcConfig |
config
Define a REST Assured Mock Mvc configuration.
|
static MockMvcRequestSpecification |
requestSpecification
Specify a default request specification that will be sent with each request.
|
static io.restassured.specification.ResponseSpecification |
responseSpecification
Specify a default response specification that will be sent with each request.
|
Constructor and Description |
---|
RestAssuredMockMvc() |
Modifier and Type | Method and Description |
---|---|
static MockMvcAuthenticationScheme |
authentication(Object authentication)
Authenticate using the supplied authentication instance (
org.springframework.security.core.Authentication from Spring Security). |
static RestAssuredMockMvcConfig |
config() |
static MockMvcResponse |
delete()
Perform a DELETE request to the statically configured base path.
|
static MockMvcResponse |
delete(String path,
Map<String,?> pathParams)
Perform a DELETE request to a
path . |
static MockMvcResponse |
delete(String path,
Object... pathParams)
Perform a DELETE request to a
path . |
static MockMvcResponse |
delete(URI uri)
Perform a DELETE request to a
uri . |
static MockMvcResponse |
delete(URL url)
Perform a DELETE request to a
url . |
static void |
enableLoggingOfRequestAndResponseIfValidationFails()
Enable logging of both the request and the response if REST Assureds test validation fails with log detail equal to
LogDetail.ALL . |
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.
|
static MockMvcResponse |
get()
Perform a GET request to the statically configured base path.
|
static MockMvcResponse |
get(String path,
Map<String,?> pathParams)
Perform a GET request to a
path . |
static MockMvcResponse |
get(String path,
Object... pathParams)
Perform a GET request to a
path . |
static MockMvcResponse |
get(URI uri)
Perform a GET request to a
uri . |
static MockMvcResponse |
get(URL url)
Perform a GET request to a
url . |
static MockMvcRequestSpecification |
given()
This is usually the entry-point of the API if you need to specify parameters or a body in the request.
|
static MockMvcResponse |
head()
Perform a HEAD request to the statically configured base path.
|
static MockMvcResponse |
head(String path,
Map<String,?> pathParams)
Perform a HEAD request to a
path . |
static MockMvcResponse |
head(String path,
Object... pathParams)
Perform a HEAD request to a
path . |
static MockMvcResponse |
head(URI uri)
Perform a HEAD request to a
uri . |
static MockMvcResponse |
head(URL url)
Perform a HEAD request to a
url . |
static void |
mockMvc(org.springframework.test.web.servlet.MockMvc mockMvc)
Set a
MockMvc instance that REST Assured will use when making requests unless overwritten
by a MockMvcRequestSpecification . |
static MockMvcResponse |
options()
Perform a OPTIONS request to the statically configured base path.
|
static MockMvcResponse |
options(String path,
Map<String,?> pathParams)
Perform a OPTIONS request to a
path . |
static MockMvcResponse |
options(String path,
Object... pathParams)
Perform a OPTIONS request to a
path . |
static MockMvcResponse |
options(URI uri)
Perform a OPTIONS request to a
uri . |
static MockMvcResponse |
options(URL url)
Perform a OPTIONS request to a
url . |
static MockMvcResponse |
patch()
Perform a PATCH request to the statically configured base path.
|
static MockMvcResponse |
patch(String path,
Map<String,?> pathParams)
Perform a PATCH request to a
path . |
static MockMvcResponse |
patch(String path,
Object... pathParams)
Perform a PATCH request to a
path . |
static MockMvcResponse |
patch(URI uri)
Perform a PATCH request to a
uri . |
static MockMvcResponse |
patch(URL url)
Perform a PATCH request to a
url . |
static MockMvcResponse |
post()
Perform a POST request to the statically configured base path.
|
static MockMvcResponse |
post(String path,
Map<String,?> pathParams)
Perform a POST request to a
path . |
static MockMvcResponse |
post(String path,
Object... pathParams)
Perform a POST request to a
path . |
static MockMvcResponse |
post(URI uri)
Perform a POST request to a
uri . |
static MockMvcResponse |
post(URL url)
Perform a POST request to a
url . |
static List<org.springframework.test.web.servlet.request.RequestPostProcessor> |
postProcessors() |
static void |
postProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor,
org.springframework.test.web.servlet.request.RequestPostProcessor... additionalPostProcessors)
Assign one or more
ResultHandler that'll be executes after a request has been made. |
static MockMvcAuthenticationScheme |
principal(Object principal)
Authenticate using the given principal.
|
static MockMvcAuthenticationScheme |
principal(Principal principal)
Authenticate using the given principal.
|
static MockMvcAuthenticationScheme |
principalWithCredentials(Object principal,
Object credentials,
String... authorities)
Authenticate using the given principal and credentials.
|
static MockMvcResponse |
put()
Perform a PUT request to the statically configured base path.
|
static MockMvcResponse |
put(String path,
Object... pathParams)
Perform a PUT request to a
path . |
static MockMvcResponse |
put(URI uri)
Perform a PUT request to a
uri . |
static MockMvcResponse |
put(URL url)
Perform a PUT request to a
url . |
static MockMvcResponse |
request(io.restassured.http.Method method)
Perform a request to the pre-configured path (by default
http://localhost:8080 ). |
static MockMvcResponse |
request(io.restassured.http.Method method,
String path,
Object... pathParams)
Perform a HTTP request to a
path . |
static MockMvcResponse |
request(io.restassured.http.Method method,
URI uri)
Perform a request to a
uri . |
static MockMvcResponse |
request(io.restassured.http.Method method,
URL url)
Perform a request to a
url . |
static MockMvcResponse |
request(String method)
Perform a custom HTTP request to the pre-configured path (by default
http://localhost:8080 ). |
static MockMvcResponse |
request(String method,
String path,
Object... pathParams)
Perform a custom HTTP request to a
path . |
static MockMvcResponse |
request(String method,
URI uri)
Perform a custom HTTP request to a
uri . |
static MockMvcResponse |
request(String method,
URL url)
Perform a custom HTTP request to a
url . |
static void |
reset()
Reset all static configurations to their default values.
|
static List<org.springframework.test.web.servlet.ResultHandler> |
resultHandlers() |
static void |
resultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler,
org.springframework.test.web.servlet.ResultHandler... resultHandlers)
Assign one or more
ResultHandler that'll be executes after a request has been made. |
static void |
standaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder)
Build a
MockMvc by using a provided AbstractMockMvcBuilder
for configuring Spring MVC infrastructure programmatically. |
static void |
standaloneSetup(Object... controllersOrMockMvcConfigurers)
Build a
MockMvc by registering one or more @Controller 's
instances and configuring Spring MVC infrastructure programmatically. |
static void |
webAppContextSetup(org.springframework.web.context.WebApplicationContext context,
org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers)
Build a
MockMvc using the given, fully initialized, i.e. |
static MockMvcRequestSender |
when()
This is usually the entry-point of the API if you need to specify parameters or a body in the request.
|
static MockMvcRequestSpecification |
with()
This is usually the entry-point of the API if you need to specify parameters or a body in the request.
|
static MockMvcAuthenticationScheme |
with(org.springframework.test.web.servlet.request.RequestPostProcessor requestPostProcessor,
org.springframework.test.web.servlet.request.RequestPostProcessor... additionalRequestPostProcessor)
Authenticate using a
RequestPostProcessor . |
public static RestAssuredMockMvcConfig config
given().config(newConfig().logConfig(new LogConfig(captor, true))). ..
newConfig()
can be statically imported from RestAssuredMockMvcConfig
.public static MockMvcRequestSpecification requestSpecification
RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().addParam("parameter1", "value1").build();means that for each request by Rest Assured "parameter1" will be equal to "value1".
public static io.restassured.specification.ResponseSpecification responseSpecification
RestAssuredMockMvc.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).build();means that for each response Rest Assured will assert that the status code is equal to 200.
public static String basePath
/
.public static MockMvcAuthenticationScheme authentication
RestAssured.authentication = principal(myPrincipal);
public static void mockMvc(org.springframework.test.web.servlet.MockMvc mockMvc)
MockMvc
instance that REST Assured will use when making requests unless overwritten
by a MockMvcRequestSpecification
.mockMvc
- The MockMvc instance to use.public static MockMvcRequestSpecification given()
given(). param("x", "y"). when(). get("/something"). then(). statusCode(200). body("x.y", notNullValue());Note that this method is the same as
with()
but with another syntax.MockMvcRequestSpecification
.public static MockMvcRequestSpecification with()
given(). param("x", "y"). when(). get("/something"). then(). statusCode(200). body("x.y", notNullValue());Note that this method is the same as
given()
but with another syntax.MockMvcRequestSpecification
.public static MockMvcRequestSender when()
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.
public static void standaloneSetup(Object... controllersOrMockMvcConfigurers)
MockMvc
by 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
org.springframework.web.servlet.DispatcherServlet
to 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.
controllersOrMockMvcConfigurers
- one or more @Controller
's to test as well
as @{link MockMvcConfigurer}'s to applypublic static void standaloneSetup(org.springframework.test.web.servlet.MockMvcBuilder builder)
MockMvc
by using a provided AbstractMockMvcBuilder
for 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.
builder
- AbstractMockMvcBuilder
to build the MVC mockpublic static void webAppContextSetup(org.springframework.web.context.WebApplicationContext context, org.springframework.test.web.servlet.setup.MockMvcConfigurer... mockMvcConfigurers)
MockMvc
using the given, fully initialized, i.e.
refreshed, WebApplicationContext
and assign it to REST Assured.
The org.springframework.web.servlet.DispatcherServlet
will use the context to discover Spring MVC infrastructure and
application controllers in it. The context must have been configured with
a ServletContext
.context
- The web application context to usemockMvcConfigurers
- MockMvcConfigurer
's to be applied when creating a MockMvc
instance of this WebApplicationContext (optional)public static void resultHandlers(org.springframework.test.web.servlet.ResultHandler resultHandler, org.springframework.test.web.servlet.ResultHandler... resultHandlers)
ResultHandler
that'll be executes after a request has been made.resultHandler
- The result handlerresultHandlers
- Additional result handlers (optional)public static List<org.springframework.test.web.servlet.ResultHandler> resultHandlers()
public static void postProcessors(org.springframework.test.web.servlet.request.RequestPostProcessor postProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalPostProcessors)
ResultHandler
that'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.
postProcessor
- a post-processor to addadditionalPostProcessors
- Additional post-processors to addMockMvcRequestSpecification.postProcessors(RequestPostProcessor, RequestPostProcessor...)
public static List<org.springframework.test.web.servlet.request.RequestPostProcessor> postProcessors()
public static void reset()
public static MockMvcResponse get(String path, Object... pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do get("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse get(String path, Map<String,?> pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters.public static MockMvcResponse post(String path, Object... pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do post("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse post(String path, Map<String,?> pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters.public static MockMvcResponse put(String path, Object... pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do put("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse delete(String path, Object... pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do delete("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse delete(String path, Map<String,?> pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters.public static MockMvcResponse head(String path, Object... pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do head("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse head(String path, Map<String,?> pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters.public static MockMvcResponse patch(String path, Object... pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do head("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse patch(String path, Map<String,?> pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters.public static MockMvcResponse options(String path, Object... pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters. E.g. if path is "/book/{hotelId}/{roomNumber}" you can do head("/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse options(String path, Map<String,?> pathParams)
path
. 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.path
- The path to send the request to.pathParams
- The path parameters.public static MockMvcResponse get(URI uri)
uri
.uri
- The uri to send the request to.public static MockMvcResponse post(URI uri)
uri
.uri
- The uri to send the request to.public static MockMvcResponse put(URI uri)
uri
.uri
- The uri to send the request to.public static MockMvcResponse delete(URI uri)
uri
.uri
- The uri to send the request to.public static MockMvcResponse head(URI uri)
uri
.uri
- The uri to send the request to.public static MockMvcResponse patch(URI uri)
uri
.uri
- The uri to send the request to.public static MockMvcResponse options(URI uri)
uri
.uri
- The uri to send the request to.public static MockMvcResponse get(URL url)
url
.url
- The url to send the request to.public static MockMvcResponse post(URL url)
url
.url
- The url to send the request to.public static MockMvcResponse put(URL url)
url
.url
- The url to send the request to.public static MockMvcResponse delete(URL url)
url
.url
- The url to send the request to.public static MockMvcResponse head(URL url)
url
.url
- The url to send the request to.public static MockMvcResponse patch(URL url)
url
.url
- The url to send the request to.public static MockMvcResponse options(URL url)
url
.url
- The url to send the request to.public static MockMvcResponse get()
public static MockMvcResponse post()
public static MockMvcResponse put()
public static MockMvcResponse delete()
public static MockMvcResponse head()
public static MockMvcResponse patch()
public static MockMvcResponse options()
public static MockMvcResponse request(io.restassured.http.Method method)
http://localhost:8080
).method
- The HTTP method to usepublic static MockMvcResponse request(String method)
http://localhost:8080
).method
- The HTTP method to usepublic static MockMvcResponse request(io.restassured.http.Method method, String path, Object... pathParams)
path
. 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.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 do request(Method.TRACE,"/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse request(String method, String path, Object... pathParams)
path
. 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.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 do request("method","/book/{hotelName}/{roomNumber}", "Hotels R Us", 22);
.public static MockMvcResponse request(io.restassured.http.Method method, URI uri)
uri
.method
- The HTTP method to useuri
- The uri to send the request to.public static MockMvcResponse request(io.restassured.http.Method method, URL url)
url
.method
- The HTTP method to useurl
- The url to send the request to.public static MockMvcResponse request(String method, URI uri)
uri
.method
- The HTTP method to useuri
- The uri to send the request to.public static MockMvcResponse request(String method, URL url)
url
.method
- The HTTP method to useurl
- The url to send the request to.public static MockMvcAuthenticationScheme principal(Principal principal)
RestAssured.authentication = principal(myPrincipal);or in a
MockMvcRequestSpecBuilder
:
MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principal(myPrincipal)). ..
principal
- The principal to use.MockMvcAuthenticationScheme
instance.MockMvcAuthenticationSpecification.principal(java.security.Principal)
public static MockMvcAuthenticationScheme principal(Object principal)
RestAssured.authentication = principal(myPrincipal);or in a
MockMvcRequestSpecBuilder
:
MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principal(myPrincipal)). ..
principal
- The principal to use.MockMvcAuthenticationScheme
instance.MockMvcAuthenticationSpecification.principal(Object)
public static MockMvcAuthenticationScheme principalWithCredentials(Object principal, Object credentials, String... authorities)
RestAssured.authentication = principalWithCredentials(myPrincipal, myCredentials);or in a
MockMvcRequestSpecBuilder
:
MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principalWithCredentials(myPrincipal, myCredentials)). ..
principal
- The principal to use.credentials
- The credentials to useauthorities
- Optional list of authoritiesMockMvcAuthenticationScheme
instance.MockMvcAuthenticationSpecification.principalWithCredentials(Object, Object, String...)
public static MockMvcAuthenticationScheme authentication(Object authentication)
org.springframework.security.core.Authentication
from Spring Security). Used as:
RestAssured.authentication = authentication(myAuth);or in a
MockMvcRequestSpecBuilder
:
MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(authentication(myAuth)). ..
authentication
- The authentication instance to use.MockMvcAuthenticationScheme
instance.MockMvcAuthenticationSpecification.authentication(Object)
public static MockMvcAuthenticationScheme with(org.springframework.test.web.servlet.request.RequestPostProcessor requestPostProcessor, org.springframework.test.web.servlet.request.RequestPostProcessor... additionalRequestPostProcessor)
RequestPostProcessor
.
This is mainly useful when you have added the spring-security-test
artifact to classpath. This allows
you to do for example:
RestAssured.authentication = with(user("username").password("password"));where
user
is statically imported from org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors
.requestPostProcessor
- The first request post processor to be used for authenticationadditionalRequestPostProcessor
- Additional request post processors to be used for authenticationMockMvcAuthenticationScheme
instance.public static void enableLoggingOfRequestAndResponseIfValidationFails()
LogDetail.ALL
.
This is just a shortcut for:
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails());
public static void enableLoggingOfRequestAndResponseIfValidationFails(io.restassured.filter.log.LogDetail logDetail)
This is just a shortcut for:
RestAssured.config = new RestAssuredMockMvcConfig().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail));
logDetail
- The log detail to show in the logpublic static RestAssuredMockMvcConfig config()
Copyright © 2010–2019. All rights reserved.