Class Assertion

java.lang.Object
org.apache.juneau.assertions.Assertion
Direct Known Subclasses:
FluentAssertion

public class Assertion extends Object
Base class for all assertion objects.
Test Methods:

  • None
Transform Methods:

  • None
Configuration Methods:

  • Constructor Details

    • Assertion

      protected Assertion(Assertion creator)
      Constructor used when this assertion is being created from within another assertion.
      Parameters:
      creator - The creator of this assertion.
  • Method Details

    • setMsg

      public Assertion setMsg(String msg, Object... args)
      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();

      Parameters:
      msg - The assertion failure message.
      args - Optional message arguments.
      Returns:
      This object.
    • setStdOut

      public Assertion setStdOut()
      If an error occurs, send the error message to STDOUT instead of STDERR.
      Returns:
      This object.
    • setOut

      public Assertion setOut(PrintStream value)
      If an error occurs, send the error message to the specified stream instead of STDERR.
      Parameters:
      value - The output stream. Can be null to suppress output.
      Returns:
      This object.
    • setSilent

      public Assertion setSilent()
      Suppresses output to STDERR.

      This is the equivalent to calling out(null).

      Returns:
      This object.
    • setThrowable

      public Assertion setThrowable(Class<? extends RuntimeException> value)
      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();

      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • error

      protected BasicAssertionError error(String msg, Object... args)
      Creates a new BasicAssertionError.
      Parameters:
      msg - The message.
      args - The message arguments.
      Returns:
      A new BasicAssertionError.
    • error

      protected BasicAssertionError error(Throwable cause, String msg, Object... args)
      Creates a new BasicAssertionError.
      Parameters:
      cause - Optional caused-by throwable.
      msg - The message.
      args - The message arguments.
      Returns:
      A new BasicAssertionError.
    • className

      protected static String className(Object o)
      Convenience method for getting the class name for an object.
      Parameters:
      o - The object to get the class name for.
      Returns:
      The class name for an object.
    • arrayClass

      protected static <E> Class<E[]> arrayClass(Class<E> c)
      Convenience method for getting the array class of the specified element type.
      Type Parameters:
      E - The element type.
      Parameters:
      c - The object to get the class name for.
      Returns:
      The class name for an object.