Class RestAssuredWebTestClient

java.lang.Object
io.restassured.module.webtestclient.RestAssuredWebTestClient

public class RestAssuredWebTestClient extends Object
The Spring Web Test Client module's equivalent of RestAssured. This is the starting point of the DSL.
  • Field Details

    • config

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

      newConfig() can be statically imported from RestAssuredWebTestClientConfig.

    • requestSpecification

      public static WebTestClientRequestSpecification requestSpecification
      Specify a default request specification that will be sent with each request. E,g.
       RestAssuredWebTestClient.requestSpecification = new WebTestClientRequestSpecBuilder().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.
       RestAssuredWebTestClient.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 /.
  • Constructor Details

    • RestAssuredWebTestClient

      public RestAssuredWebTestClient()
  • Method Details

    • webTestClient

      public static void webTestClient(org.springframework.test.web.reactive.server.WebTestClient webTestClient)
      Set a WebTestClient instance that REST Assured will use when making requests unless overwritten by a WebTestClientRequestSpecification.
      Parameters:
      webTestClient - The WebTestClient instance to use.
    • given

      public static WebTestClientRequestSpecification 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 WebTestClientRequestSpecification.
    • with

      public static WebTestClientRequestSpecification with()
      This is usually the entry-point of the API if you need to specify parameters or a body in the request. For example: Note that this method is the same as given() but with another syntax.
      Returns:
      A WebTestClientRequestSpecification.
    • standaloneSetup

      public static void standaloneSetup(Object... controllersOrConfigurersOrExchangeFilterFunctions)
      Build a WebTestClient by registering one or more @Controller's instances and configuring WebTestClient programmatically. This allows full control over the instantiation, configuration and initialization of controllers, and their dependencies, similar to plain unit tests while also making it possible to test one controller at a time.

      It uses WebTestClient.bindToController(Object...) under the hood. It also allows you to pass WebTestClientConfigurer and ExchangeFilterFunction instances that are used to set up the WebTestClient instance.

      Parameters:
      controllersOrConfigurersOrExchangeFilterFunctions - one or more @Controllers to test, as well as WebTestClientConfigurers and ExchangeFilterFunctions to apply.
    • standaloneSetup

      public static void standaloneSetup(org.springframework.web.reactive.function.server.RouterFunction routerFunction, Object... configurersOrExchangeFilterFunctions)
      Build a WebTestClient by using a provided RouterFunction for configuring WebTestClient programmatically. This allows full control over the instantiation, configuration and initialization of router functions, and their dependencies, similar to plain unit tests while also making it possible to test one router function at a time.

      It uses WebTestClient.bindToRouterFunction(RouterFunction) under the hood. It also allows you to pass WebTestClientConfigurer and ExchangeFilterFunction instances that are used to set up the WebTestClient instance

      Parameters:
      routerFunction - RouterFunction to build WebTestClient.
      configurersOrExchangeFilterFunctions - WebTestClientConfigurers and ExchangeFilterFunctions to apply.
    • standaloneSetup

      public static void standaloneSetup(org.springframework.test.web.reactive.server.WebTestClient.Builder builder)
      Build a WebTestClient by using a provided WebTestClient.Builder for configuring WebTestClient 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.
      Parameters:
      builder - WebTestClient.Builder to build WebTestClient.
    • webAppContextSetup

      public static void webAppContextSetup(org.springframework.web.context.WebApplicationContext context, Object... configurersOrExchangeFilterFunctions)
      Build a WebTestClient using the given, fully initialized, i.e. refreshed, WebApplicationContext and assign it to REST Assured.

      The passed WebApplicationContext will be used as ApplicationContext.

      Parameters:
      context - The web application context to use
      configurersOrExchangeFilterFunctions - WebTestClientConfigurers and ExchangeFilterFunctions to apply.
    • applicationContextSetup

      public static void applicationContextSetup(org.springframework.context.ApplicationContext context, Object... configurersOrExchangeFilterFunctions)
      Build a WebTestClient using the given, fully initialized, i.e. refreshed, ApplicationContext and assign it to REST Assured.
      Parameters:
      context - The application context to use
      configurersOrExchangeFilterFunctions - WebTestClientConfigurers and ExchangeFilterFunctions to apply.
    • reset

      public static void reset()
      Reset all static configurations to their default values.
    • get

      public static WebTestClientResponse 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 WebTestClientResponse 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.
    • get

      public static WebTestClientResponse get(Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a GET request to a path generated from the provided Function uriFunction.
      Parameters:
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the GET request.
    • post

      public static WebTestClientResponse 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.
    • get

      public static WebTestClientResponse 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.
    • get

      public static WebTestClientResponse 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.
    • get

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

      public static WebTestClientResponse 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 WebTestClientResponse 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 WebTestClientResponse 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 WebTestClientResponse 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 WebTestClientResponse 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 WebTestClientResponse request(io.restassured.http.Method method, Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a request to a path generated from the provided Function uriFunction.
      Parameters:
      method - The HTTP method to use
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the request.
    • request

      public static WebTestClientResponse 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 WebTestClientResponse 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 WebTestClientResponse request(String method, Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a request to a path generated from the provided Function uriFunction.
      Parameters:
      method - The HTTP method to use
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the request.
    • request

      public static WebTestClientResponse 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.
    • post

      public static WebTestClientResponse 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 WebTestClientResponse 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.
    • post

      public static WebTestClientResponse post(Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a POST request to a path generated from the provided Function uriFunction.
      Parameters:
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the POST request.
    • post

      public static WebTestClientResponse 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.
    • post

      public static WebTestClientResponse 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.
    • post

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

      public static WebTestClientResponse 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.
    • put

      public static WebTestClientResponse put(String path, Map<String,?> pathParams)
    • put

      public static WebTestClientResponse put(Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a PUT request to a path generated from the provided Function uriFunction.
      Parameters:
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the PUT request.
    • put

      public static WebTestClientResponse 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.
    • put

      public static WebTestClientResponse put(URL url)
    • put

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

      public static WebTestClientResponse 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 WebTestClientResponse 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.
    • delete

      public static WebTestClientResponse delete(Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a DELETE request to a path generated from the provided Function uriFunction.
      Parameters:
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the DELETE request.
    • delete

      public static WebTestClientResponse 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.
    • delete

      public static WebTestClientResponse 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.
    • delete

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

      public static WebTestClientResponse 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 WebTestClientResponse 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.
    • head

      public static WebTestClientResponse head(Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a HEAD request to a path generated from the provided Function uriFunction.
      Parameters:
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the HEAD request.
    • head

      public static WebTestClientResponse 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.
    • head

      public static WebTestClientResponse 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.
    • head

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

      public static WebTestClientResponse 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.
    • patch

      public static WebTestClientResponse patch(Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a PATCH request to a path generated from the provided Function uriFunction.
      Parameters:
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the PATCH request.
    • patch

      public static WebTestClientResponse 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.
    • patch

      public static WebTestClientResponse 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.
    • patch

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

      public static WebTestClientResponse 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 WebTestClientResponse 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.
    • options

      public static WebTestClientResponse options(Function<org.springframework.web.util.UriBuilder,URI> uriFunction)
      Perform a OPTIONS request to a path generated from the provided Function uriFunction.
      Parameters:
      uriFunction - The Function<UriBuilder, URI> used to generate the path to send the request to.
      Returns:
      The response of the OPTIONS request.
    • options

      public static WebTestClientResponse 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.
    • options

      public static WebTestClientResponse 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.
    • options

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

      public static void enableLoggingOfRequestAndResponseIfValidationFails()
      Enable logging of both the request and the response if REST Assured test validation fails with log detail equal to LogDetail.ALL.

      This is just a shortcut for:

       RestAssuredWebTestClient.config = new RestAssuredWebTestClientConfig().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 Assured test validation fails with the specified log detail.

      This is just a shortcut for:

       RestAssured.config = new RestAssuredWebTestClientConfig().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail));
       
      Parameters:
      logDetail - The log detail to show in the log
    • 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 lets you call resources on the server.
    • config

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