Class AbstractComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>

java.lang.Object
org.assertj.core.api.AbstractAssert<SELF,ACTUAL>
org.assertj.core.api.AbstractObjectAssert<SELF,ACTUAL>
org.assertj.core.api.AbstractComparableAssert<SELF,ACTUAL>
Type Parameters:
SELF - the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
ACTUAL - the type of the "actual" value.
All Implemented Interfaces:
Assert<SELF,ACTUAL>, ComparableAssert<SELF,ACTUAL>, Descriptable<SELF>, ExtensionPoints<SELF,ACTUAL>
Direct Known Subclasses:
AbstractBigDecimalAssert, AbstractBigIntegerAssert, AbstractByteAssert, AbstractCharacterAssert, AbstractDoubleAssert, AbstractDurationAssert, AbstractFloatAssert, AbstractIntegerAssert, AbstractLongAssert, AbstractPathAssert, AbstractShortAssert, AbstractUriAssert, GenericComparableAssert

public abstract class AbstractComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>> extends AbstractObjectAssert<SELF,ACTUAL> implements ComparableAssert<SELF,ACTUAL>
Base class for all implementations of ComparableAssert.
Author:
Alex Ruiz, Mikhail Mazursky
  • Constructor Details

    • AbstractComparableAssert

      protected AbstractComparableAssert(ACTUAL actual, Class<?> selfType)
  • Method Details

    • isEqualByComparingTo

      public SELF isEqualByComparingTo(ACTUAL other)
      Verifies that the actual value is equal to the given one by invoking Comparable.compareTo(Object).

      Example:

       // assertion will pass
       assertThat(1.0).isEqualByComparingTo(1.0);
       // assertion will pass because 8.0 is equal to 8.00 using BigDecimal.compareTo(BigDecimal)
       assertThat(new BigDecimal("8.0")).isEqualByComparingTo(new BigDecimal("8.00"));
      
       // assertion will fail
       assertThat(new BigDecimal(1.0)).isEqualByComparingTo(new BigDecimal(2.0));
      Specified by:
      isEqualByComparingTo in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
    • isNotEqualByComparingTo

      public SELF isNotEqualByComparingTo(ACTUAL other)
      Verifies that the actual value is not equal to the given one by invoking Comparable.compareTo(Object).

      Example:

       // assertion will pass
       assertThat(new BigDecimal(1.0)).isNotEqualByComparingTo(new BigDecimal(2.0));
      
       // assertion will fail
       assertThat(1.0).isNotEqualByComparingTo(1.0);
       // assertion will fail because 8.0 is equal to 8.00 using BigDecimal.compareTo(BigDecimal)
       assertThat(new BigDecimal("8.0")).isNotEqualByComparingTo(new BigDecimal("8.00"));
      Specified by:
      isNotEqualByComparingTo in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
    • isLessThan

      public SELF isLessThan(ACTUAL other)
      Verifies that the actual value is less than the given one.

      Example:

       // assertions will pass
       assertThat('a').isLessThan('b');
       assertThat(BigInteger.ZERO).isLessThan(BigInteger.ONE);
       
       // assertions will fail
       assertThat('a').isLessThan('a');
       assertThat(BigInteger.ONE).isLessThan(BigInteger.ZERO);
      Specified by:
      isLessThan in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
    • isLessThanOrEqualTo

      public SELF isLessThanOrEqualTo(ACTUAL other)
      Verifies that the actual value is less than or equal to the given one.

      Example:

       // assertions will pass
       assertThat('a').isLessThanOrEqualTo('b');
       assertThat('a').isLessThanOrEqualTo('a');
       assertThat(BigInteger.ZERO).isLessThanOrEqualTo(BigInteger.ZERO);
       
       // assertions will fail
       assertThat('b').isLessThanOrEqualTo('a');
       assertThat(BigInteger.ONE).isLessThanOrEqualTo(BigInteger.ZERO);
      Specified by:
      isLessThanOrEqualTo in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
    • isGreaterThan

      public SELF isGreaterThan(ACTUAL other)
      Verifies that the actual value is greater than the given one.

      Example:

       // assertions will pass
       assertThat('b').isGreaterThan('a');
       assertThat(BigInteger.ONE).isGreaterThan(BigInteger.ZERO);
       
       // assertions will fail
       assertThat('b').isGreaterThan('a');
       assertThat(BigInteger.ZERO).isGreaterThan(BigInteger.ZERO);
      Specified by:
      isGreaterThan in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
    • isGreaterThanOrEqualTo

      public SELF isGreaterThanOrEqualTo(ACTUAL other)
      Verifies that the actual value is greater than or equal to the given one.

      Example:

       // assertions will pass
       assertThat('b').isGreaterThanOrEqualTo('a');
       assertThat(BigInteger.ONE).isGreaterThanOrEqualTo(BigInteger.ONE);
       
       // assertions will fail
       assertThat('a').isGreaterThanOrEqualTo('b');
       assertThat(BigInteger.ZERO).isGreaterThanOrEqualTo(BigInteger.ONE);
      Specified by:
      isGreaterThanOrEqualTo in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
    • isBetween

      public SELF isBetween(ACTUAL startInclusive, ACTUAL endInclusive)
      Verifies that the actual value is in [start, end] range (start included, end included).

      Example:

       // assertions succeed
       assertThat('b').isBetween('a', 'c');
       assertThat('a').isBetween('a', 'b');
       assertThat('b').isBetween('a', 'b');
       
       // assertions fail
       assertThat('a').isBetween('b', 'c');
       assertThat('c').isBetween('a', 'b');
      Specified by:
      isBetween in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      startInclusive - the start value (inclusive), expected not to be null.
      endInclusive - the end value (inclusive), expected not to be null.
      Returns:
      this assertion object.
    • isStrictlyBetween

      public SELF isStrictlyBetween(ACTUAL startExclusive, ACTUAL endExclusive)
      Verifies that the actual value is in ]start, end[ range (start excluded, end excluded).

      Example:

       // assertion succeeds
       assertThat('b').isStrictlyBetween('a', 'c');
       
       // assertions fail
       assertThat('d').isStrictlyBetween('a', 'c');
       assertThat('a').isStrictlyBetween('b', 'd');
       assertThat('a').isStrictlyBetween('a', 'b');
       assertThat('b').isStrictlyBetween('a', 'b');
      Specified by:
      isStrictlyBetween in interface ComparableAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      startExclusive - the start value (exclusive), expected not to be null.
      endExclusive - the end value (exclusive), expected not to be null.
      Returns:
      this assertion object.
    • usingComparator

      public SELF usingComparator(Comparator<? super ACTUAL> customComparator)
      Description copied from class: AbstractAssert
      Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.

      The custom comparator is bound to assertion instance, meaning that if a new assertion instance is created, the default comparison strategy will be used.

      Examples :

       // frodo and sam are instances of Character with Hobbit race (obviously :).
       // raceComparator implements Comparator<Character>
       assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam);
      Specified by:
      usingComparator in interface Assert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Overrides:
      usingComparator in class AbstractAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      customComparator - the comparator to use for the incoming assertion checks.
      Returns:
      this assertion object.
    • usingComparator

      public SELF usingComparator(Comparator<? super ACTUAL> customComparator, String customComparatorDescription)
      Description copied from class: AbstractAssert
      Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.

      The custom comparator is bound to assertion instance, meaning that if a new assertion instance is created, the default comparison strategy will be used.

      Examples :

       // frodo and sam are instances of Character with Hobbit race (obviously :).
       // raceComparator implements Comparator<Character>
       assertThat(frodo).usingComparator(raceComparator, "Hobbit Race Comparator").isEqualTo(sam);
      Specified by:
      usingComparator in interface Assert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Overrides:
      usingComparator in class AbstractAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Parameters:
      customComparator - the comparator to use for the incoming assertion checks.
      customComparatorDescription - comparator description to be used in assertion error messages
      Returns:
      this assertion object.
    • usingDefaultComparator

      public SELF usingDefaultComparator()
      Description copied from class: AbstractAssert
      Revert to standard comparison for the incoming assertion checks.

      This method should be used to disable a custom comparison strategy set by calling usingComparator.

      Specified by:
      usingDefaultComparator in interface Assert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Overrides:
      usingDefaultComparator in class AbstractAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Returns:
      this assertion object.
    • inHexadecimal

      public SELF inHexadecimal()
      Description copied from class: AbstractAssert
      Use hexadecimal object representation instead of standard representation in error messages.

      It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned, it is thus impossible to find differences from the standard error message:

      With standard message:

       assertThat("µµµ").contains("μμμ");
      
       java.lang.AssertionError:
       Expecting:
         <"µµµ">
       to contain:
         <"μμμ">
      With Hexadecimal message:
       assertThat("µµµ").inHexadecimal().contains("μμμ");
      
       java.lang.AssertionError:
       Expecting:
         <"['00B5', '00B5', '00B5']">
       to contain:
         <"['03BC', '03BC', '03BC']">
      Overrides:
      inHexadecimal in class AbstractAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Returns:
      this assertion object.
    • inBinary

      public SELF inBinary()
      Description copied from class: AbstractAssert
      Use binary object representation instead of standard representation in error messages.

      Example:

       assertThat(1).inBinary().isEqualTo(2);
      
       org.junit.ComparisonFailure:
       Expected :0b00000000_00000000_00000000_00000010
       Actual   :0b00000000_00000000_00000000_00000001
      Overrides:
      inBinary in class AbstractAssert<SELF extends AbstractComparableAssert<SELF,ACTUAL>,ACTUAL extends Comparable<? super ACTUAL>>
      Returns:
      this assertion object.