Class ShouldBeEqual

  • All Implemented Interfaces:
    AssertionErrorFactory

    public class ShouldBeEqual
    extends java.lang.Object
    implements AssertionErrorFactory
    Creates an AssertionError indicating that an assertion that verifies that two objects are equal failed.

    The built AssertionError's message differentiates actual and expected description if their string representation are the same (e.g. 42 float and 42 double). It also mentions the comparator in case of a custom comparator is used (instead of equals method).

    • Field Detail

      • EXPECTED_BUT_WAS_MESSAGE

        private static final java.lang.String EXPECTED_BUT_WAS_MESSAGE
        See Also:
        Constant Field Values
      • EXPECTED_BUT_WAS_MESSAGE_USING_COMPARATOR

        private static final java.lang.String EXPECTED_BUT_WAS_MESSAGE_USING_COMPARATOR
        See Also:
        Constant Field Values
      • MSG_ARG_TYPES

        private static final java.lang.Class<?>[] MSG_ARG_TYPES
      • actual

        protected final java.lang.Object actual
      • expected

        protected final java.lang.Object expected
    • Constructor Detail

    • Method Detail

      • shouldBeEqual

        public static AssertionErrorFactory shouldBeEqual​(java.lang.Object actual,
                                                          java.lang.Object expected,
                                                          Representation representation)
        Creates a new ShouldBeEqual.
        Parameters:
        actual - the actual value in the failed assertion.
        expected - the expected value in the failed assertion.
        Returns:
        the created AssertionErrorFactory.
      • shouldBeEqual

        public static AssertionErrorFactory shouldBeEqual​(java.lang.Object actual,
                                                          java.lang.Object expected,
                                                          ComparisonStrategy comparisonStrategy,
                                                          Representation representation)
        Creates a new ShouldBeEqual.
        Parameters:
        actual - the actual value in the failed assertion.
        expected - the expected value in the failed assertion.
        comparisonStrategy - the ComparisonStrategy used to compare actual with expected.
        Returns:
        the created AssertionErrorFactory.
      • newAssertionError

        public java.lang.AssertionError newAssertionError​(Description description,
                                                          Representation representation)
        Creates an AssertionError indicating that an assertion that verifies that two objects are equal failed.
        The AssertionError message is built so that it differentiates actual and expected description in case their string representation are the same (like 42 float and 42 double).

        If JUnit 4 is in the classpath and the description is standard (no comparator was used and actual and expected string representation were different), this method will instead create a org.junit.ComparisonFailure that highlights the difference(s) between the expected and actual objects.

        AssertionError stack trace won't show AssertJ related elements if Failures is configured to filter them (see Failures.setRemoveAssertJRelatedElementsFromStackTrace(boolean)).
        Specified by:
        newAssertionError in interface AssertionErrorFactory
        Parameters:
        description - the description of the failed assertion.
        representation -
        Returns:
        the created AssertionError.
      • actualAndExpectedHaveSameStringRepresentation

        private boolean actualAndExpectedHaveSameStringRepresentation()
      • defaultErrorMessage

        private java.lang.String defaultErrorMessage​(Description description,
                                                     Representation representation)
        Builds and returns an error message from description using expected and actual basic representation.
        Parameters:
        description - the Description used to build the returned error message
        representation - the Representation used to build String representation of object
        Returns:
        the error message from description using expected and actual basic representation.
      • comparisonFailure

        private java.lang.AssertionError comparisonFailure​(Description description)
      • newComparisonFailure

        private java.lang.AssertionError newComparisonFailure​(java.lang.String description)
                                                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • msgArgs

        private java.lang.Object[] msgArgs​(java.lang.String description)
      • detailedActual

        private java.lang.String detailedActual()
      • detailedExpected

        private java.lang.String detailedExpected()
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object