Class FluentObjectAssertion<T,R>

Type Parameters:
T - The object type.
R - The return type.
Direct Known Subclasses:
FluentAnyAssertion, FluentArrayAssertion, FluentBeanAssertion, FluentCollectionAssertion, FluentComparableAssertion, FluentMapAssertion, FluentPrimitiveArrayAssertion, FluentProtocolVersionAssertion, FluentRequestContentAssertion, FluentRequestLineAssertion, FluentResponseBodyAssertion, FluentResponseStatusLineAssertion, FluentStringAssertion, FluentThrowableAssertion, ObjectAssertion

public class FluentObjectAssertion<T,R> extends FluentAssertion<R>
  • Constructor Details

    • FluentObjectAssertion

      public FluentObjectAssertion(T value, R returns)
      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.
    • FluentObjectAssertion

      public FluentObjectAssertion(Assertion creator, T 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

      Converts this object to a string using Object.toString() and returns it as a new assertion.
      Example:

      // Validates that the specified object is "foobar" after converting to a string. assertObject(myPojo) .asString() .is("foobar");

      Returns:
      A new fluent string assertion.
    • asString

      Converts this object to text using the specified serializer and returns it as a new assertion.
      Example:

      // Validates that the specified object is an instance of MyBean. assertObject(myPojo) .asString(XmlSerializer.DEFAULT) .is("<object><foo>bar</foo><baz>qux</baz></object>");

      Parameters:
      ws - The serializer to use to convert the object to text.
      Returns:
      A new fluent string assertion.
    • asString

      Converts this object to a string using the specified function and returns it as a new assertion.
      Example:

      // Validates that the specified object is "foobar" after converting to a string. assertObject(myPojo) .asString(x->x.toString()) .is("foobar");

      Parameters:
      function - The conversion function.
      Returns:
      A new fluent string assertion.
    • asJson

      Converts this object to simplified JSON and returns it as a new assertion.
      Example:

      // Validates that the specified object is an instance of MyBean. assertObject(myPojo) .asJson() .is("{foo:'bar',baz:'qux'}");

      Returns:
      A new fluent string assertion.
    • asJsonSorted

      Converts this object to sorted simplified JSON and returns it as a new assertion.
      Example:

      // Validates that the specified object is an instance of MyBean. assertObject(myPojo) .asJsonSorted() .is("{baz:'qux',foo:'bar'}");

      Returns:
      A new fluent string assertion.
    • asTransformed

      Applies a transform on the inner object and returns a new inner object.
      Parameters:
      function - The function to apply.
      Returns:
      This object.
    • asTransformedTo

      public <T2> FluentObjectAssertion<T2,R> asTransformedTo(Function<T,T2> function)
      Applies a transform on the inner object and returns a new inner object.
      Type Parameters:
      T2 - The transform-to type.
      Parameters:
      function - The function to apply.
      Returns:
      This object.
    • asAny

      Converts this assertion into an FluentAnyAssertion so that it can be converted to other assertion types.
      Returns:
      This object.
    • isExists

      public R isExists() throws AssertionError
      Asserts that the object is not null.

      Equivalent to isNotNull().

      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isNull

      public R isNull() throws AssertionError
      Asserts that the object i null.

      Equivalent to isNotNull().

      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isNotNull

      public R isNotNull() throws AssertionError
      Asserts that the object is not null.

      Equivalent to isNotNull().

      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • is

      public R is(T value) throws AssertionError
      Asserts that the value equals the specified value.
      Parameters:
      value - The value to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isString

      public R isString(String value)
      Asserts that the value converted to a string equals the specified value.
      Parameters:
      value - The value to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isNot

      public R isNot(T value) throws AssertionError
      Asserts that the value does not equal the specified value.
      Parameters:
      value - The value to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isAny

      public R isAny(T... values) throws AssertionError
      Asserts that the value is one of the specified values.
      Parameters:
      values - The values to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isNotAny

      public R isNotAny(T... values) throws AssertionError
      Asserts that the value is not one of the specified values.
      Parameters:
      values - The values to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isSame

      public R isSame(T value) throws AssertionError
      Asserts that the specified object is the same object as this object.
      Parameters:
      value - The value to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isSameJsonAs

      public R isSameJsonAs(Object o) throws AssertionError
      Verifies that two objects are equivalent after converting them both to JSON.
      Parameters:
      o - The object to compare against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isSameSortedJsonAs

      Verifies that two objects are equivalent after converting them both to sorted JSON.

      Properties, maps, and collections are all sorted on both objects before comparison.

      Parameters:
      o - The object to compare against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isSameSerializedAs

      public R isSameSerializedAs(Object o, WriterSerializer serializer)
      Asserts that the specified object is the same as this object after converting both to strings using the specified serializer.
      Parameters:
      o - The object to compare against.
      serializer - The serializer to use to serialize this object.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isType

      public R isType(Class<?> parent) throws AssertionError
      Asserts that the object is an instance of the specified class.
      Example:

      // Validates that the specified object is an instance of MyBean. assertObject(myPojo).isType(MyBean.class);

      Parameters:
      parent - The value to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isExactType

      public R isExactType(Class<?> type) throws AssertionError
      Asserts that the object is an instance of the specified class.
      Example:

      // Validates that the specified object is an instance of MyBean. assertObject(myPojo).isExactType(MyBean.class);

      Parameters:
      type - The value to check against.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • is

      public R is(Predicate<T> test) throws AssertionError
      Asserts that the value passes the specified predicate test.
      Parameters:
      test - The predicate to use to test the value.
      Returns:
      The fluent return object.
      Throws:
      AssertionError - If assertion failed.
    • isJson

      public R isJson(String value)
      Converts this object to simplified JSON and runs the FluentStringAssertion.is(String) on the result.
      Example:

      // Validates that the specified object is an instance of MyBean. assertObject(myPojo) .asJson() .is("{foo:'bar',baz:'qux'}");

      Parameters:
      value - The expected string value.
      Returns:
      The fluent return object.
    • setMsg

      public FluentObjectAssertion<T,R> setMsg(String msg, Object... args)
      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 FluentAssertion<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 FluentAssertion<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 FluentAssertion<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 FluentAssertion<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 FluentAssertion<R>
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • value

      protected T value() throws AssertionError
      Returns the inner value after asserting it is not null.
      Returns:
      The inner value.
      Throws:
      AssertionError - If inner value was null.
    • valueAsString

      protected String valueAsString()
      Returns the inner value as a string.
      Returns:
      The inner value as a string, or null if the value was null.
    • orElse

      protected T orElse(T other)
      Returns the inner value or the other value if the value is null.
      Parameters:
      other - The other value.
      Returns:
      The inner value.
    • valueIsNull

      protected boolean valueIsNull()
      Returns true if the inner value is null.
      Returns:
      true if the inner value is null.
    • valueIsNotNull

      protected boolean valueIsNotNull()
      Returns true if the inner value is not null.
      Returns:
      true if the inner value is not null.
    • opt

      protected Optional<T> opt()
      Returns the value wrapped in an Optional.
      Returns:
      The value wrapped in an Optional.
    • map

      protected <T2> Optional<T2> map(Function<? super T,? extends T2> mapper)
      Returns the result of running the specified function against the value and returns the result.
      Type Parameters:
      T2 - The mapper-to type.
      Parameters:
      mapper - The function to run against the value.
      Returns:
      The result, never null.
    • getFailureMessage

      protected String getFailureMessage(Predicate<?> p, Object value)
      Returns the predicate failure message.

      If the predicate extends from AssertionPredicate, then the message comes from AssertionPredicate.getFailureMessage(). Otherwise, returns a generic "Unexpected value: x" message.

      Parameters:
      p - The function to run against the value.
      value - The value that failed the test.
      Returns:
      The result, never null.
    • equals

      protected boolean equals(Object o1, Object o2)
      Checks two objects for equality.
      Parameters:
      o1 - The first object.
      o2 - The second object.
      Returns:
      true if the objects are equal.
    • toString

      public String toString()
      Returns the string form of the inner object. Subclasses can override this method to affect the asString() method (and related).
      Overrides:
      toString in class Object