Interface WebTestClient


public interface WebTestClient
Client for testing web servers that uses WebClient internally to perform requests while also providing a fluent API to verify responses. This client can connect to any server over HTTP, or to a WebFlux application via mock request and response objects.

Use one of the bindToXxx methods to create an instance. For example:

Since:
5.0
Author:
Rossen Stoyanchev, Brian Clozel, Sam Brannen, MichaƂ Rowicki
See Also:
  • Field Details

    • WEBTESTCLIENT_REQUEST_ID

      static final String WEBTESTCLIENT_REQUEST_ID
      The name of a request header used to assign a unique id to every request performed through the WebTestClient. This can be useful for storing contextual information at all phases of request processing (e.g. from a server-side component) under that id and later to look up that information once an ExchangeResult is available.
      See Also:
  • Method Details

    • get

      Prepare an HTTP GET request.
      Returns:
      a spec for specifying the target URL
    • head

      Prepare an HTTP HEAD request.
      Returns:
      a spec for specifying the target URL
    • post

      Prepare an HTTP POST request.
      Returns:
      a spec for specifying the target URL
    • put

      Prepare an HTTP PUT request.
      Returns:
      a spec for specifying the target URL
    • patch

      Prepare an HTTP PATCH request.
      Returns:
      a spec for specifying the target URL
    • delete

      Prepare an HTTP DELETE request.
      Returns:
      a spec for specifying the target URL
    • options

      Prepare an HTTP OPTIONS request.
      Returns:
      a spec for specifying the target URL
    • method

      Prepare a request for the specified HttpMethod.
      Returns:
      a spec for specifying the target URL
    • mutate

      Return a builder to mutate properties of this web test client.
    • mutateWith

      WebTestClient mutateWith(WebTestClientConfigurer configurer)
      Mutate the WebTestClient, apply the given configurer, and build a new instance. Essentially a shortcut for:
       mutate().apply(configurer).build();
       
      Parameters:
      configurer - the configurer to apply
      Returns:
      the mutated test client
    • bindToController

      static WebTestClient.ControllerSpec bindToController(Object... controllers)
      Use this server setup to test one @Controller at a time. This option loads the default configuration of @EnableWebFlux. There are builder methods to customize the Java config. The resulting WebFlux application will be tested without an HTTP server using a mock request and response.
      Parameters:
      controllers - one or more controller instances to test (specified Class will be turned into instance)
      Returns:
      chained API to customize server and client config; use WebTestClient.MockServerSpec.configureClient() to transition to client config
    • bindToRouterFunction

      static WebTestClient.RouterFunctionSpec bindToRouterFunction(RouterFunction<?> routerFunction)
      Use this option to set up a server from a RouterFunction. Internally the provided configuration is passed to RouterFunctions#toWebHandler. The resulting WebFlux application will be tested without an HTTP server using a mock request and response.
      Parameters:
      routerFunction - the RouterFunction to test
      Returns:
      chained API to customize server and client config; use WebTestClient.MockServerSpec.configureClient() to transition to client config
    • bindToApplicationContext

      static WebTestClient.MockServerSpec<?> bindToApplicationContext(ApplicationContext applicationContext)
      Use this option to set up a server from the Spring configuration of your application, or some subset of it. Internally the provided configuration is passed to WebHttpHandlerBuilder to set up the request processing chain. The resulting WebFlux application will be tested without an HTTP server using a mock request and response.

      Consider using the TestContext framework and @ContextConfiguration in order to efficiently load and inject the Spring configuration into the test class.

      Parameters:
      applicationContext - the Spring context
      Returns:
      chained API to customize server and client config; use WebTestClient.MockServerSpec.configureClient() to transition to client config
    • bindToWebHandler

      static WebTestClient.MockServerSpec<?> bindToWebHandler(WebHandler webHandler)
      Integration testing with a "mock" server targeting the given WebHandler.
      Parameters:
      webHandler - the handler to test
      Returns:
      chained API to customize server and client config; use WebTestClient.MockServerSpec.configureClient() to transition to client config
    • bindToServer

      static WebTestClient.Builder bindToServer()
      This server setup option allows you to connect to a live server through a Reactor Netty client connector.

       WebTestClient client = WebTestClient.bindToServer()
               .baseUrl("http://localhost:8080")
               .build();
       
      Returns:
      chained API to customize client config
    • bindToServer

      static WebTestClient.Builder bindToServer(ClientHttpConnector connector)
      A variant of bindToServer() with a pre-configured connector.
      Returns:
      chained API to customize client config
      Since:
      5.0.2