Class RestAssuredMockMvc

java.lang.Object
io.restassured.module.mockmvc.RestAssuredMockMvc

public class RestAssuredMockMvc extends Object
The Spring MVC module's equivalent of RestAssured. This is the starting point of the DSL.

Note that some Javadoc is copied from Spring MVC's test documentation.

  • Field Details

    • config

      public static RestAssuredMockMvcConfig config
      Define a REST Assured Mock Mvc configuration. E.g.
       given().config(newConfig().logConfig(new LogConfig(captor, true))). ..
       

      newConfig() can be statically imported from RestAssuredMockMvcConfig.

    • requestSpecification

      public static MockMvcRequestSpecification 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 responseSpecification
      Specify 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

      public static String 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

      public static MockMvcAuthenticationScheme authentication
      Defines a global authentication scheme that'll be used for all requests (if not overridden). Usage example:
       RestAssured.authentication = principal(myPrincipal);
       
      See Also:
  • Constructor Details

    • RestAssuredMockMvc

      public RestAssuredMockMvc()
  • Method Details

    • mockMvc

      public 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.
      Parameters:
      mockMvc - The MockMvc instance to use.
    • given

      public static MockMvcRequestSpecification 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 as with() but with another syntax.
      Returns:
      A MockMvcRequestSpecification.
    • with

      public static 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 as given() but with another syntax.

      Returns:
      A MockMvcRequestSpecification.
    • when

      public static MockMvcRequestSender 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

      public static void standaloneSetup(Object... controllersOrMockMvcConfigurers)
      Build a 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 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.

      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 a 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.

      Parameters:
      builder - AbstractMockMvcBuilder to build the MVC mock
    • webAppContextSetup

      public 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. refreshed, WebApplicationContext and assign it to REST Assured. The DispatcherServlet will use the context to discover Spring MVC infrastructure and application controllers in it. The context must have been configured with a ServletContext.
      Parameters:
      context - The web application context to use
      mockMvcConfigurers - MockMvcConfigurer's to be applied when creating a MockMvc instance 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 more ResultHandler that'll be executes after a request has been made.
      Parameters:
      resultHandler - The result handler
      resultHandlers - Additional result handlers (optional)
    • resultHandlers

      public static List<org.springframework.test.web.servlet.ResultHandler> 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 more 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.

      Parameters:
      postProcessor - a post-processor to add
      additionalPostProcessors - 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

      public static MockMvcResponse get(String path, Object... pathParams)
      Perform a GET request to a 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.
      Parameters:
      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);.
      Returns:
      The response of the GET request.
    • get

      public static MockMvcResponse get(String path, Map<String,?> pathParams)
      Perform a GET request to a 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.
      Parameters:
      path - The path to send the request to.
      pathParams - The path parameters.
      Returns:
      The response of the GET request.
    • post

      public static MockMvcResponse post(String path, Object... pathParams)
      Perform a POST request to a 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.
      Parameters:
      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);.
      Returns:
      The response of the request.
    • post

      public static MockMvcResponse post(String path, Map<String,?> pathParams)
      Perform a POST request to a 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.
      Parameters:
      path - The path to send the request to.
      pathParams - The path parameters.
      Returns:
      The response of the request.
    • put

      public static MockMvcResponse put(String path, Object... pathParams)
      Perform a PUT request to a 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.
      Parameters:
      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);.
      Returns:
      The response of the request.
    • delete

      public static MockMvcResponse delete(String path, Object... pathParams)
      Perform a DELETE request to a 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.
      Parameters:
      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);.
      Returns:
      The response of the request.
    • delete

      public static MockMvcResponse delete(String path, Map<String,?> pathParams)
      Perform a DELETE request to a 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.
      Parameters:
      path - The path to send the request to.
      pathParams - The path parameters.
      Returns:
      The response of the request.
    • head

      public static MockMvcResponse head(String path, Object... pathParams)
      Perform a HEAD request to a 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.
      Parameters:
      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);.
      Returns:
      The response of the request.
    • head

      public static MockMvcResponse head(String path, Map<String,?> pathParams)
      Perform a HEAD request to a 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.
      Parameters:
      path - The path to send the request to.
      pathParams - The path parameters.
      Returns:
      The response of the request.
    • patch

      public static MockMvcResponse patch(String path, Object... pathParams)
      Perform a PATCH request to a 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.
      Parameters:
      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);.
      Returns:
      The response of the request.
    • patch

      public static MockMvcResponse patch(String path, Map<String,?> pathParams)
      Perform a PATCH request to a 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.
      Parameters:
      path - The path to send the request to.
      pathParams - The path parameters.
      Returns:
      The response of the request.
    • options

      public static MockMvcResponse options(String path, Object... pathParams)
      Perform a OPTIONS request to a 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.
      Parameters:
      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);.
      Returns:
      The response of the request.
    • options

      public static MockMvcResponse options(String path, Map<String,?> pathParams)
      Perform a OPTIONS request to a 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.
      Parameters:
      path - The path to send the request to.
      pathParams - The path parameters.
      Returns:
      The response of the request.
    • get

      public static MockMvcResponse get(URI uri)
      Perform a GET request to a uri.
      Parameters:
      uri - The uri to send the request to.
      Returns:
      The response of the GET request.
    • post

      public static MockMvcResponse post(URI uri)
      Perform a POST request to a uri.
      Parameters:
      uri - The uri to send the request to.
      Returns:
      The response of the request.
    • put

      public static MockMvcResponse put(URI uri)
      Perform a PUT request to a uri.
      Parameters:
      uri - The uri to send the request to.
      Returns:
      The response of the request.
    • delete

      public static MockMvcResponse delete(URI uri)
      Perform a DELETE request to a uri.
      Parameters:
      uri - The uri to send the request to.
      Returns:
      The response of the request.
    • head

      public static MockMvcResponse head(URI uri)
      Perform a HEAD request to a uri.
      Parameters:
      uri - The uri to send the request to.
      Returns:
      The response of the request.
    • patch

      public static MockMvcResponse patch(URI uri)
      Perform a PATCH request to a uri.
      Parameters:
      uri - The uri to send the request to.
      Returns:
      The response of the request.
    • options

      public static MockMvcResponse options(URI uri)
      Perform a OPTIONS request to a uri.
      Parameters:
      uri - The uri to send the request to.
      Returns:
      The response of the request.
    • get

      public static MockMvcResponse get(URL url)
      Perform a GET request to a url.
      Parameters:
      url - The url to send the request to.
      Returns:
      The response of the GET request.
    • post

      public static MockMvcResponse post(URL url)
      Perform a POST request to a url.
      Parameters:
      url - The url to send the request to.
      Returns:
      The response of the request.
    • put

      public static MockMvcResponse put(URL url)
      Perform a PUT request to a url.
      Parameters:
      url - The url to send the request to.
      Returns:
      The response of the request.
    • delete

      public static MockMvcResponse delete(URL url)
      Perform a DELETE request to a url.
      Parameters:
      url - The url to send the request to.
      Returns:
      The response of the request.
    • head

      public static MockMvcResponse head(URL url)
      Perform a HEAD request to a url.
      Parameters:
      url - The url to send the request to.
      Returns:
      The response of the request.
    • patch

      public static MockMvcResponse patch(URL url)
      Perform a PATCH request to a url.
      Parameters:
      url - The url to send the request to.
      Returns:
      The response of the request.
    • options

      public static MockMvcResponse options(URL url)
      Perform a OPTIONS request to a url.
      Parameters:
      url - The url to send the request to.
      Returns:
      The response of the request.
    • get

      public static MockMvcResponse get()
      Perform a GET request to the statically configured base path.
      Returns:
      The response of the GET request.
    • post

      public static MockMvcResponse post()
      Perform a POST request to the statically configured base path.
      Returns:
      The response of the request.
    • put

      public static MockMvcResponse put()
      Perform a PUT request to the statically configured base path.
      Returns:
      The response of the request.
    • delete

      public static MockMvcResponse delete()
      Perform a DELETE request to the statically configured base path.
      Returns:
      The response of the request.
    • head

      public static MockMvcResponse head()
      Perform a HEAD request to the statically configured base path.
      Returns:
      The response of the request.
    • patch

      public static MockMvcResponse patch()
      Perform a PATCH request to the statically configured base path.
      Returns:
      The response of the request.
    • options

      public static MockMvcResponse options()
      Perform a OPTIONS request to the statically configured base path.
      Returns:
      The response of the request.
    • request

      public static MockMvcResponse request(io.restassured.http.Method method)
      Perform a request to the pre-configured path (by default http://localhost:8080).
      Parameters:
      method - The HTTP method to use
      Returns:
      The response of the request.
    • request

      public static MockMvcResponse request(String method)
      Perform a custom HTTP request to the pre-configured path (by default http://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 a 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.
      Parameters:
      method - The HTTP method to use
      path - 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);.
      Returns:
      The response of the request.
    • request

      public static MockMvcResponse request(String method, String path, Object... pathParams)
      Perform a custom HTTP request to a 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.
      Parameters:
      method - The HTTP method to use
      path - 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);.
      Returns:
      The response of the request.
    • request

      public static MockMvcResponse request(io.restassured.http.Method method, URI uri)
      Perform a request to a uri.
      Parameters:
      method - The HTTP method to use
      uri - The uri to send the request to.
      Returns:
      The response of the GET request.
    • request

      public static MockMvcResponse request(io.restassured.http.Method method, URL url)
      Perform a request to a url.
      Parameters:
      method - The HTTP method to use
      url - The url to send the request to.
      Returns:
      The response of the GET request.
    • request

      public static MockMvcResponse request(String method, URI uri)
      Perform a custom HTTP request to a uri.
      Parameters:
      method - The HTTP method to use
      uri - The uri to send the request to.
      Returns:
      The response of the GET request.
    • request

      public static MockMvcResponse request(String method, URL url)
      Perform a custom HTTP request to a url.
      Parameters:
      method - The HTTP method to use
      url - The url to send the request to.
      Returns:
      The response of the GET request.
    • principal

      public static MockMvcAuthenticationScheme principal(Principal principal)
      Authenticate using the given principal. Used as:
       RestAssured.authentication = principal(myPrincipal);
       
      or in a MockMvcRequestSpecBuilder:
       MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principal(myPrincipal)). ..
       
      Parameters:
      principal - The principal to use.
      Returns:
      A MockMvcAuthenticationScheme instance.
      See Also:
    • principal

      public static MockMvcAuthenticationScheme principal(Object principal)
      Authenticate using the given principal. Used as:
       RestAssured.authentication = principal(myPrincipal);
       
      or in a MockMvcRequestSpecBuilder:
       MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principal(myPrincipal)). ..
       
      Parameters:
      principal - The principal to use.
      Returns:
      A MockMvcAuthenticationScheme instance.
      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 a MockMvcRequestSpecBuilder:
       MockMvcRequestSpecification req = new MockMvcRequestSpecBuilder().setAuth(principalWithCredentials(myPrincipal, myCredentials)). ..
       
      Parameters:
      principal - The principal to use.
      credentials - The credentials to use
      authorities - Optional list of authorities
      Returns:
      A MockMvcAuthenticationScheme instance.
      See Also:
    • authentication

      public static MockMvcAuthenticationScheme authentication(Object authentication)
      Authenticate using the supplied authentication instance (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)). ..
       
      Parameters:
      authentication - The authentication instance to use.
      Returns:
      A MockMvcAuthenticationScheme instance.
      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 a 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.
      Parameters:
      requestPostProcessor - The first request post processor to be used for authentication
      additionalRequestPostProcessor - Additional request post processors to be used for authentication
      Returns:
      A MockMvcAuthenticationScheme instance.
    • enableLoggingOfRequestAndResponseIfValidationFails

      public 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.

      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

      public static RestAssuredMockMvcConfig config()
      Returns:
      The assigned config or a new config is no config is assigned