Class AssertionFailureBuilder

java.lang.Object
org.junit.jupiter.api.AssertionFailureBuilder

@API(status=STABLE, since="5.9") public class AssertionFailureBuilder extends Object
Builder for AssertionFailedErrors.

Using this builder ensures consistency in how failure message are formatted within JUnit Jupiter and for custom user-defined assertions.

Since:
5.9
See Also:
  • AssertionFailedError
  • Field Details

    • message

      private Object message
    • cause

      private Throwable cause
    • mismatch

      private boolean mismatch
    • expected

      private Object expected
    • actual

      private Object actual
    • reason

      private String reason
    • includeValuesInMessage

      private boolean includeValuesInMessage
  • Constructor Details

    • AssertionFailureBuilder

      private AssertionFailureBuilder()
  • Method Details

    • assertionFailure

      public static AssertionFailureBuilder assertionFailure()
      Create a new AssertionFailureBuilder.
    • message

      public AssertionFailureBuilder message(Object message)
      Set the user-defined message of the assertion.

      The message may be passed as a Supplier or plain String. If any other type is passed, it is converted to String as per StringUtils.nullSafeToString(Object).

      Parameters:
      message - the user-defined failure message; may be null
      Returns:
      this builder for method chaining
    • reason

      public AssertionFailureBuilder reason(String reason)
      Set the reason why the assertion failed.
      Parameters:
      reason - the failure reason; may be null
      Returns:
      this builder for method chaining
    • cause

      public AssertionFailureBuilder cause(Throwable cause)
      Set the cause of the assertion failure.
      Parameters:
      cause - the failure cause; may be null
      Returns:
      this builder for method chaining
    • expected

      public AssertionFailureBuilder expected(Object expected)
      Set the expected value of the assertion.
      Parameters:
      expected - the expected value; may be null
      Returns:
      this builder for method chaining
    • actual

      public AssertionFailureBuilder actual(Object actual)
      Set the actual value of the assertion.
      Parameters:
      actual - the actual value; may be null
      Returns:
      this builder for method chaining
    • includeValuesInMessage

      public AssertionFailureBuilder includeValuesInMessage(boolean includeValuesInMessage)
      Set whether to include the actual and expected values in the generated failure message.
      Parameters:
      includeValuesInMessage - whether to include the actual and expected values
      Returns:
      this builder for method chaining
    • buildAndThrow

      public void buildAndThrow() throws org.opentest4j.AssertionFailedError
      Build the AssertionFailedError and throw it.
      Throws:
      org.opentest4j.AssertionFailedError - always
    • build

      public org.opentest4j.AssertionFailedError build()
      Build the AssertionFailedError without throwing it.
      Returns:
      the built assertion failure
    • nullSafeGet

      private static String nullSafeGet(Object messageOrSupplier)
    • buildPrefix

      private static String buildPrefix(String message)
    • formatValues

      private static String formatValues(Object expected, Object actual)
    • formatClassAndValue

      private static String formatClassAndValue(Object value, String valueString)
    • toString

      private static String toString(Object obj)
    • toHash

      private static String toHash(Object obj)
    • getClassName

      private static String getClassName(Object obj)