Record Class LogAssertionsUtil.ThrowableSpec

java.lang.Object
java.lang.Record
nl.pim16aap2.testing.logging.LogAssertionsUtil.ThrowableSpec
Record Components:
expectedType - The expected type of the exception.
expectedMessage - The expected message of the exception.
stringCompareMethod - The method to use when comparing the expected and actual messages.
Enclosing class:
LogAssertionsUtil

public static record LogAssertionsUtil.ThrowableSpec(Class<?> expectedType, @Nullable String expectedMessage, LogAssertionsUtil.MessageComparisonMethod stringCompareMethod) extends Record
Represents a specification for an expected exception.

This is used to specify the expected type of an exception and its message.

If the message is null, it will not be checked when comparing this specification to a throwable.

  • Constructor Details

    • ThrowableSpec

      public ThrowableSpec(Class<?> expectedType, @Nullable @Nullable String expectedMessage)
      Creates a new instance of the exception specification.

      Defaults to LogAssertionsUtil.MessageComparisonMethod.EQUALS for the message comparison.

      Parameters:
      expectedType - The expected type of the exception.
      expectedMessage - The expected message of the exception.
    • ThrowableSpec

      public ThrowableSpec(Class<?> expectedType)
      Creates a new instance of the exception specification.

      Sets the expected message to null, meaning it will not be checked when comparing this specification to a throwable.

      Parameters:
      expectedType - The expected type of the exception.
    • ThrowableSpec

      public ThrowableSpec(Class<?> expectedType, @Nullable @Nullable String expectedMessage, LogAssertionsUtil.MessageComparisonMethod stringCompareMethod)
      Creates an instance of a ThrowableSpec record class.
      Parameters:
      expectedType - the value for the expectedType record component
      expectedMessage - the value for the expectedMessage record component
      stringCompareMethod - the value for the stringCompareMethod record component
  • Method Details

    • matches

      @Contract(value="null -> false", pure=true) public boolean matches(@Nullable @Nullable Throwable throwable)
      Checks whether the throwable matches the specification.
      Parameters:
      throwable - The throwable to check.
      Returns:
      True if the throwable matches the specification, false otherwise.
    • messageMatches

      public boolean messageMatches(@Nullable @Nullable String message)
      Checks whether the message matches the expected message.

      If the expected message is null, this method will always return true.

      Parameters:
      message - The message to check.
      Returns:
      True if the message matches, false otherwise.
    • of

      public static LogAssertionsUtil.ThrowableSpec[] of(Class<?>... classes)
      Creates an array of ThrowableSpecs from the provided classes.

      See ThrowableSpec(Class).

      Parameters:
      classes - The classes to create the ThrowableSpecs from.
      Returns:
      The array of ThrowableSpecs.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • expectedType

      public Class<?> expectedType()
      Returns the value of the expectedType record component.
      Returns:
      the value of the expectedType record component
    • expectedMessage

      @Nullable public @Nullable String expectedMessage()
      Returns the value of the expectedMessage record component.
      Returns:
      the value of the expectedMessage record component
    • stringCompareMethod

      public LogAssertionsUtil.MessageComparisonMethod stringCompareMethod()
      Returns the value of the stringCompareMethod record component.
      Returns:
      the value of the stringCompareMethod record component