Class FluentResponseBodyAssertion<R>

Type Parameters:
R - The return type.

  • Constructor Details

    • FluentResponseBodyAssertion

      Constructor.
      Parameters:
      value - The object being tested.
      Can be null.
      returns - The object to return after a test method is called.
      If null, the test method returns this object allowing multiple test method calls to be used on the same assertion.
    • FluentResponseBodyAssertion

      public FluentResponseBodyAssertion(Assertion creator, ResponseContent value, R returns)
      Chained constructor.

      Used when transforming one assertion into another so that the assertion config can be used by the new assertion.

      Parameters:
      creator - The assertion that created this assertion.
      Should be null if this is the top-level assertion.
      value - The object being tested.
      Can be null.
      returns - The object to return after a test method is called.
      If null, the test method returns this object allowing multiple test method calls to be used on the same assertion.
  • Method Details

    • asString

      Provides the ability to perform fluent-style assertions on this response body.
      Examples:

      // Validates the response body equals the text "OK". client .get(URI) .run() .assertContent().is("OK"); // Validates the response body contains the text "OK". client .get(URI) .run() .assertContent().isContains("OK"); // Validates the response body passes a predicate test. client .get(URI) .run() .assertContent().is(x -> x.contains("OK")); // Validates the response body matches a regular expression. client .get(URI) .run() .assertContent().isPattern(".*OK.*"); // Validates the response body matches a regular expression using regex flags. client .get(URI) .run() .assertContent().isPattern(".*OK.*", MULTILINE & CASE_INSENSITIVE); // Validates the response body matches a regular expression in the form of an existing Pattern. Pattern pattern = Pattern.compile(".*OK.*"); client .get(URI) .run() .assertContent().isPattern(pattern);

      The assertion test returns the original response object allowing you to chain multiple requests like so:

      // Validates the response body matches a regular expression. MyBean bean = client .get(URI) .run() .assertContent().isPattern(".*OK.*"); .assertContent().isNotPattern(".*ERROR.*") .getContent().as(MyBean.class);

      Notes:
      • If no charset was found on the Content-Type response header, "UTF-8" is assumed.
      • When using this method, the body is automatically cached by calling the ResponseContent.cache().
      • The input stream is automatically closed after this call.
      Overrides:
      asString in class FluentObjectAssertion<ResponseContent,R>
      Returns:
      A new fluent assertion object.
    • asBytes

      Provides the ability to perform fluent-style assertions on the bytes of the response body.
      Examples:

      // Validates the response body equals the text "foo". client .get(URI) .run() .assertContent().asBytes().asHex().is("666F6F");

      Notes:
      • If no charset was found on the Content-Type response header, "UTF-8" is assumed.
      • When using this method, the body is automatically cached by calling the ResponseContent.cache().
      • The input stream is automatically closed after this call.
      Returns:
      A new fluent assertion object.
    • as

      public <T> FluentAnyAssertion<T,R> as(Class<T> type)
      Converts the body to a type using ResponseContent.as(Class) and then returns the value as an object assertion.
      Examples:

      // Validates the response body as a list of strings and validates the length. client .get("/myBean") .run() .assertContent().as(List.class, String.class).is(x -> x.size() > 0);

      Type Parameters:
      T - The object type to create.
      Parameters:
      type - The object type to create.
      Returns:
      A new fluent assertion object.
    • as

      public FluentAnyAssertion<Object,R> as(Type type, Type... args)
      Converts the body to a type using ResponseContent.as(Type,Type...) and then returns the value as an object assertion.
      Examples:

      // Validates the response body as a list of strings and validates the length. client .get("/myBean") .run() .assertContent().as(List.class, String.class).is(x -> x.size() > 0);

      See Complex Data Types for information on defining complex generic types of Maps and Collections.

      Parameters:
      type - The object type to create.
      args - Optional type arguments.
      Returns:
      A new fluent assertion object.
    • is

      public R is(String value) throws AssertionError
      Asserts that the body contains the specified value.
      Parameters:
      value - The value to check against.
      Returns:
      This object.
      Throws:
      AssertionError - If assertion failed.
    • isContains

      public R isContains(String... values) throws AssertionError
      Asserts that the text contains all of the specified substrings.
      Parameters:
      values - The values to check against.
      Returns:
      This object.
      Throws:
      AssertionError - If assertion failed.
    • isNotContains

      public R isNotContains(String... values) throws AssertionError
      Asserts that the body doesn't contain any of the specified substrings.
      Parameters:
      values - The values to check against.
      Returns:
      This object.
      Throws:
      AssertionError - If assertion failed.
    • isEmpty

      public R isEmpty()
      Asserts that the body is empty.
      Returns:
      This object.
      Throws:
      AssertionError - If assertion failed.
    • isNotEmpty

      public R isNotEmpty()
      Asserts that the body is not empty.
      Returns:
      This object.
      Throws:
      AssertionError - If assertion failed.
    • valueAsString

      protected String valueAsString() throws AssertionError
      Description copied from class: FluentObjectAssertion
      Returns the inner value as a string.
      Overrides:
      valueAsString in class FluentObjectAssertion<ResponseContent,R>
      Returns:
      The inner value as a string, or null if the value was null.
      Throws:
      AssertionError
    • setMsg

      Description copied from class: Assertion
      Allows you to override the assertion failure message.

      String can contain "{msg}" to represent the original message.

      Example:

      import static org.apache.juneau.assertions.Assertions.*; // Throws an assertion with a custom message instead of the default "Value was null." assertString(myString) .setMsg("My string was bad: {msg}") .isNotNull();

      Overrides:
      setMsg in class FluentObjectAssertion<ResponseContent,R>
      Parameters:
      msg - The assertion failure message.
      args - Optional message arguments.
      Returns:
      This object.
    • setOut

      Description copied from class: Assertion
      If an error occurs, send the error message to the specified stream instead of STDERR.
      Overrides:
      setOut in class FluentObjectAssertion<ResponseContent,R>
      Parameters:
      value - The output stream. Can be null to suppress output.
      Returns:
      This object.
    • setSilent

      Description copied from class: Assertion
      Suppresses output to STDERR.

      This is the equivalent to calling out(null).

      Overrides:
      setSilent in class FluentObjectAssertion<ResponseContent,R>
      Returns:
      This object.
    • setStdOut

      Description copied from class: Assertion
      If an error occurs, send the error message to STDOUT instead of STDERR.
      Overrides:
      setStdOut in class FluentObjectAssertion<ResponseContent,R>
      Returns:
      This object.
    • setThrowable

      Description copied from class: Assertion
      If an error occurs, throw this exception instead of the standard AssertionError.

      The throwable class must have a public constructor that takes in any of the following parameters:

      • Throwable - The caused-by exception (if there is one).
      • String - The assertion failure message.

      If the throwable cannot be instantiated, a RuntimeException is thrown instead.

      Example:

      import static org.apache.juneau.assertions.Assertions.*; // Throws a BadRequest instead of an AssertionError if the string is null. assertString(myString) .setThrowable(BadRequest.class) .isNotNull();

      Overrides:
      setThrowable in class FluentObjectAssertion<ResponseContent,R>
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.