Class ResponseAssertion

java.lang.Object
org.apache.juneau.rest.client.assertion.ResponseAssertion

public final class ResponseAssertion extends Object
A fluent assertion class for validating RestResponse instances.

Provides assertion methods for HTTP status code, response headers, and body content. All assertion methods return this for chaining, and throw AssertionError on failure.

Obtain instances via RestResponse.assertThat().

try (RestResponse resp = client.get("/api/users").run()) { resp.assertThat() .statusCode(200) .header("Content-Type").contains("application/json") .body().contains("alice"); }

See Also:
Since:
9.2.1
  • Constructor Details

    • ResponseAssertion

      public ResponseAssertion(RestResponse response)
      Constructor.
      Parameters:
      response - The response to assert on. Must not be null.
  • Method Details

    • statusCode

      public ResponseAssertion statusCode(int expected)
      Asserts that the response status code equals the expected value.
      Parameters:
      expected - The expected status code.
      Returns:
      This object (for chaining).
      Throws:
      AssertionError - If the status code does not match.
    • isOk

      Asserts that the response status code is in the 2xx (success) range.
      Returns:
      This object (for chaining).
      Throws:
      AssertionError - If the status code is not in 200–299.
    • isClientError

      Asserts that the response status code is in the 4xx (client error) range.
      Returns:
      This object (for chaining).
      Throws:
      AssertionError - If the status code is not in 400–499.
    • isServerError

      Asserts that the response status code is in the 5xx (server error) range.
      Returns:
      This object (for chaining).
      Throws:
      AssertionError - If the status code is not in 500–599.
    • header

      Returns a fluent header assertion for the named response header.
      Parameters:
      name - The header name (case-insensitive). Must not be null.
      Returns:
      A new header assertion. Never null.
    • body

      Returns a fluent body assertion.
      Returns:
      A new body assertion. Never null.
    • and

      public RestResponse and()
      Returns the underlying response (for further access after assertions).
      Returns:
      The response. Never null.