Interface WebTestClient.ResponseSpec

Enclosing interface:
WebTestClient

public static interface WebTestClient.ResponseSpec
Chained API for applying assertions to a response.
  • Method Details

    • expectAll

      Apply multiple assertions to a response with the given consumers, with the guarantee that all assertions will be applied even if one or more assertions fails with an exception.

      If a single Error or RuntimeException is thrown, it will be rethrown.

      If multiple exceptions are thrown, this method will throw an AssertionError whose error message is a summary of all of the exceptions. In addition, each exception will be added as a suppressed exception to the AssertionError.

      This feature is similar to the SoftAssertions support in AssertJ and the assertAll() support in JUnit Jupiter.

      Example

       webTestClient.get().uri("/hello").exchange()
           .expectAll(
               responseSpec -> responseSpec.expectStatus().isOk(),
               responseSpec -> responseSpec.expectBody(String.class).isEqualTo("Hello, World!")
           );
       
      Parameters:
      consumers - the list of ResponseSpec consumers
      Since:
      5.3.10
    • expectStatus

      StatusAssertions expectStatus()
      Assertions on the response status.
    • expectHeader

      HeaderAssertions expectHeader()
      Assertions on the headers of the response.
    • expectCookie

      CookieAssertions expectCookie()
      Assertions on the cookies of the response.
      Since:
      5.3
    • expectBody

      <B> WebTestClient.BodySpec<B,?> expectBody(Class<B> bodyType)
      Consume and decode the response body to a single object of type <B> and then apply assertions.
      Parameters:
      bodyType - the expected body type
    • expectBody

      <B> WebTestClient.BodySpec<B,?> expectBody(ParameterizedTypeReference<B> bodyType)
      Alternative to expectBody(Class) that accepts information about a target type with generics.
    • expectBodyList

      <E> WebTestClient.ListBodySpec<E> expectBodyList(Class<E> elementType)
      Consume and decode the response body to List<E> and then apply List-specific assertions.
      Parameters:
      elementType - the expected List element type
    • expectBodyList

      <E> WebTestClient.ListBodySpec<E> expectBodyList(ParameterizedTypeReference<E> elementType)
      Alternative to expectBodyList(Class) that accepts information about a target type with generics.
    • expectBody

      Consume and decode the response body to byte[] and then apply assertions on the raw content (e.g. isEmpty, JSONPath, etc.)
    • returnResult

      <T> FluxExchangeResult<T> returnResult(Class<T> elementClass)
      Exit the chained flow in order to consume the response body externally, e.g. via StepVerifier.

      Note that when Void.class is passed in, the response body is consumed and released. If no content is expected, then consider using .expectBody().isEmpty() instead which asserts that there is no content.

    • returnResult

      <T> FluxExchangeResult<T> returnResult(ParameterizedTypeReference<T> elementTypeRef)
      Alternative to returnResult(Class) that accepts information about a target type with generics.