Class AbstractUniversalComparableAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,T>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,ACTUAL>
-
- org.assertj.core.api.AbstractObjectAssert<SELF,java.lang.Comparable<T>>
-
- org.assertj.core.api.AbstractUniversalComparableAssert<SELF,T>
-
- All Implemented Interfaces:
Assert<SELF,java.lang.Comparable<T>>
,Descriptable<SELF>
,ExtensionPoints<SELF,java.lang.Comparable<T>>
- Direct Known Subclasses:
UniversalComparableAssert
public abstract class AbstractUniversalComparableAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,T> extends AbstractObjectAssert<SELF,java.lang.Comparable<T>>
Base class forComparable
assertions.This class offers better compatibility than
ComparableAssert
and related implementations, currently limited due to the upper bound ofComparableAssert
's type parameters.Let's take an example with a class
Name
implementingComparable<Name>
.Comparable<Name> name1 = new Name("abc");
The following does not compile or work as expected:
// does not compile as assertThat(name1) resolves to Object assertions assertThat(name1).isLessThanOrEqualTo(name1); // compiles fine but requires a raw Comparable cast (assertThat resolves to AbstractComparableAssert) assertThat((Comparable)name1).isLessThanOrEqualTo(name1); // does not compile: Cannot infer type arguments for GenericComparableAssert<> new GenericComparableAssert<>(name1).isLessThanOrEqualTo(name3); // compiles fine with the concrete type (assertThat resolves to AbstractComparableAssert) Name name = name1; assertThat(name).isEqualByComparingTo(name);
This class aims to allow writing
// assertThatComparable resolves to AbstractUniversalComparableAssert assertThatComparable(name1).isLessThanOrEqualTo(name1); // it works with the concrete type too assertThatComparable(name).isEqualByComparingTo(name);
- Since:
- 3.23.0
- See Also:
Assertions.assertThatComparable(Comparable)
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Comparables
comparables
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, customRepresentation, info, myself, objects, printAssertionsDescription, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractUniversalComparableAssert(java.lang.Comparable<T> actual, java.lang.Class<?> selfType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SELF
inBinary()
Use binary object representation instead of standard representation in error messages.SELF
inHexadecimal()
Use hexadecimal object representation instead of standard representation in error messages.SELF
isBetween(T startInclusive, T endInclusive)
Verifies that the actual value is in [start, end] range (start included, end included).SELF
isEqualByComparingTo(T other)
Verifies that the actual value is equal to the given one by invoking
.Comparable.compareTo(Object)
SELF
isGreaterThan(T other)
Verifies that the actual value is greater than the given one.SELF
isGreaterThanOrEqualTo(T other)
Verifies that the actual value is greater than or equal to the given one.SELF
isLessThan(T other)
Verifies that the actual value is less than the given one.SELF
isLessThanOrEqualTo(T other)
Verifies that the actual value is less than or equal to the given one.SELF
isNotEqualByComparingTo(T other)
Verifies that the actual value is not equal to the given one by invoking
.Comparable.compareTo(Object)
SELF
isStrictlyBetween(T startExclusive, T endExclusive)
Verifies that the actual value is in ]start, end[ range (start excluded, end excluded).SELF
usingComparator(java.util.Comparator<? super java.lang.Comparable<T>> customComparator)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.SELF
usingComparator(java.util.Comparator<? super java.lang.Comparable<T>> customComparator, java.lang.String customComparatorDescription)
Use the given custom comparator instead of relying on actual type A equals method for incoming assertion checks.SELF
usingDefaultComparator()
Revert to standard comparison for the incoming assertion checks.-
Methods inherited from class org.assertj.core.api.AbstractObjectAssert
as, as, doesNotReturn, extracting, extracting, extracting, extracting, extracting, extracting, extractingForProxy, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, hasOnlyFields, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison, withAssertionState, withComparatorByPropertyOrField, withTypeComparator
-
Methods inherited from class org.assertj.core.api.AbstractAssert
areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnError
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.Descriptable
as, describedAs, describedAs
-
-
-
-
Field Detail
-
comparables
Comparables comparables
-
-
Constructor Detail
-
AbstractUniversalComparableAssert
protected AbstractUniversalComparableAssert(java.lang.Comparable<T> actual, java.lang.Class<?> selfType)
-
-
Method Detail
-
isEqualByComparingTo
public SELF isEqualByComparingTo(T other)
Verifies that the actual value is equal to the given one by invoking
.Comparable.compareTo(Object)
Example:
// assertion will pass assertThatComparable(1.0).isEqualByComparingTo(1.0); // assertion will pass because 8.0 is equal to 8.00 using
BigDecimal.compareTo(BigDecimal)
assertThatComparable(new BigDecimal("8.0")).isEqualByComparingTo(new BigDecimal("8.00")); // assertion will fail assertThatComparable(new BigDecimal(1.0)).isEqualByComparingTo(new BigDecimal(2.0));- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is not equal when comparing to the given one.
-
isNotEqualByComparingTo
public SELF isNotEqualByComparingTo(T other)
Verifies that the actual value is not equal to the given one by invoking
.Comparable.compareTo(Object)
Example:
// assertion will pass assertThatComparable(new BigDecimal(1.0)).isNotEqualByComparingTo(new BigDecimal(2.0)); // assertion will fail assertThatComparable(1.0).isNotEqualByComparingTo(1.0); // assertion will fail because 8.0 is equal to 8.00 using
BigDecimal.compareTo(BigDecimal)
assertThatComparable(new BigDecimal("8.0")).isNotEqualByComparingTo(new BigDecimal("8.00"));- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is equal when comparing to the given one.
-
isLessThan
public SELF isLessThan(T other)
Verifies that the actual value is less than the given one.Example:
// assertions will pass assertThatComparable('a').isLessThan('b'); assertThatComparable(BigInteger.ZERO).isLessThan(BigInteger.ONE); // assertions will fail assertThatComparable('a').isLessThan('a'); assertThatComparable(BigInteger.ONE).isLessThan(BigInteger.ZERO);
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is equal to or greater than the given one.
-
isLessThanOrEqualTo
public SELF isLessThanOrEqualTo(T other)
Verifies that the actual value is less than or equal to the given one.Example:
// assertions will pass assertThatComparable('a').isLessThanOrEqualTo('b'); assertThatComparable('a').isLessThanOrEqualTo('a'); assertThatComparable(BigInteger.ZERO).isLessThanOrEqualTo(BigInteger.ZERO); // assertions will fail assertThatComparable('b').isLessThanOrEqualTo('a'); assertThatComparable(BigInteger.ONE).isLessThanOrEqualTo(BigInteger.ZERO);
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is greater than the given one.
-
isGreaterThan
public SELF isGreaterThan(T other)
Verifies that the actual value is greater than the given one.Example:
// assertions will pass assertThatComparable('b').isGreaterThan('a'); assertThatComparable(BigInteger.ONE).isGreaterThan(BigInteger.ZERO); // assertions will fail assertThatComparable('b').isGreaterThan('a'); assertThatComparable(BigInteger.ZERO).isGreaterThan(BigInteger.ZERO);
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is equal to or less than the given one.
-
isGreaterThanOrEqualTo
public SELF isGreaterThanOrEqualTo(T other)
Verifies that the actual value is greater than or equal to the given one.Example:
// assertions will pass assertThatComparable('b').isGreaterThanOrEqualTo('a'); assertThatComparable(BigInteger.ONE).isGreaterThanOrEqualTo(BigInteger.ONE); // assertions will fail assertThatComparable('a').isGreaterThanOrEqualTo('b'); assertThatComparable(BigInteger.ZERO).isGreaterThanOrEqualTo(BigInteger.ONE);
- Parameters:
other
- the given value to compare the actual value to.- Returns:
this
assertion object.- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.AssertionError
- if the actual value is less than the given one.
-
isBetween
public SELF isBetween(T startInclusive, T endInclusive)
Verifies that the actual value is in [start, end] range (start included, end included).Example:
// assertions succeed assertThatComparable('b').isBetween('a', 'c'); assertThatComparable('a').isBetween('a', 'b'); assertThatComparable('b').isBetween('a', 'b'); // assertions fail assertThatComparable('a').isBetween('b', 'c'); assertThatComparable('c').isBetween('a', 'b');
- 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.
- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.NullPointerException
- if start value isnull
.java.lang.NullPointerException
- if end value isnull
.java.lang.AssertionError
- if the actual value is not in [start, end] range.
-
isStrictlyBetween
public SELF isStrictlyBetween(T startExclusive, T endExclusive)
Verifies that the actual value is in ]start, end[ range (start excluded, end excluded).Example:
// assertion succeeds assertThatComparable('b').isStrictlyBetween('a', 'c'); // assertions fail assertThatComparable('d').isStrictlyBetween('a', 'c'); assertThatComparable('a').isStrictlyBetween('b', 'd'); assertThatComparable('a').isStrictlyBetween('a', 'b'); assertThatComparable('b').isStrictlyBetween('a', 'b');
- 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.
- Throws:
java.lang.AssertionError
- if the actual value isnull
.java.lang.NullPointerException
- if start value isnull
.java.lang.NullPointerException
- if end value isnull
.java.lang.AssertionError
- if the actual value is not in ]start, end[ range.
-
usingComparator
public SELF usingComparator(java.util.Comparator<? super java.lang.Comparable<T>> customComparator)
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 interfaceAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,T>
- Overrides:
usingComparator
in classAbstractAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,java.lang.Comparable<T>>
- Parameters:
customComparator
- the comparator to use for the incoming assertion checks.- Returns:
this
assertion object.
-
usingComparator
public SELF usingComparator(java.util.Comparator<? super java.lang.Comparable<T>> customComparator, java.lang.String customComparatorDescription)
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 interfaceAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,T>
- Overrides:
usingComparator
in classAbstractAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,java.lang.Comparable<T>>
- 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()
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 interfaceAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,T>
- Overrides:
usingDefaultComparator
in classAbstractAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,java.lang.Comparable<T>>
- Returns:
this
assertion object.
-
inHexadecimal
public SELF inHexadecimal()
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:
With Hexadecimal message:assertThat("µµµ").contains("μμμ"); java.lang.AssertionError: Expecting: <"µµµ"> to contain: <"μμμ">
assertThat("µµµ").inHexadecimal().contains("μμμ"); java.lang.AssertionError: Expecting: <"['00B5', '00B5', '00B5']"> to contain: <"['03BC', '03BC', '03BC']">
- Overrides:
inHexadecimal
in classAbstractAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,java.lang.Comparable<T>>
- Returns:
this
assertion object.
-
inBinary
public SELF inBinary()
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 classAbstractAssert<SELF extends AbstractUniversalComparableAssert<SELF,T>,java.lang.Comparable<T>>
- Returns:
this
assertion object.
-
-