Class AbstractIntegerAssert<SELF extends AbstractIntegerAssert<SELF>>

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.
All Implemented Interfaces:
Assert<SELF,Integer>, ComparableAssert<SELF,Integer>, Descriptable<SELF>, ExtensionPoints<SELF,Integer>, NumberAssert<SELF,Integer>
Direct Known Subclasses:
AbstractBigDecimalScaleAssert, AbstractIterableSizeAssert, AbstractMapSizeAssert, IntegerAssert

public abstract class AbstractIntegerAssert<SELF extends AbstractIntegerAssert<SELF>> extends AbstractComparableAssert<SELF,Integer> implements NumberAssert<SELF,Integer>
Base class for all implementations of assertions for Integers.
Author:
Drummond Dawson, Yvonne Wang, David DIDIER, Ansgar Konermann, Alex Ruiz, Joel Costigliola, Mikhail Mazursky, Nicolas François, Cal027
  • Constructor Details

    • AbstractIntegerAssert

      protected AbstractIntegerAssert(Integer actual, Class<?> selfType)
  • Method Details

    • isEqualTo

      public SELF isEqualTo(int expected)
      Verifies that the actual value is equal to the given one.

      Example:

       // assertions will pass:
       assertThat(1).isEqualTo(1);
       assertThat(-1).isEqualTo(-1);
      
       // assertions will fail:
       assertThat(1).isEqualTo(2);
       assertThat(1).isEqualTo(-1);
      Parameters:
      expected - the given value to compare the actual value to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is not equal to the given one.
    • isEqualTo

      public SELF isEqualTo(long expected)
      Verifies that the actual value is equal to the given long. If the long value is not in [Integer.MIN_VALUE, Integer.MAX_VALUE], the assertion simply fails.

      Example:

       // assertions will pass:
       assertThat(1).isEqualTo(1L);
       assertThat(-1).isEqualTo(-1L);
      
       // assertions will fail:
       assertThat(1).isEqualTo(2L);
       assertThat(1).isEqualTo(-1L);
      Parameters:
      expected - the given value to compare the actual value to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is not equal to the given one.
      Since:
      3.10.0
    • isNotEqualTo

      public SELF isNotEqualTo(int other)
      Verifies that the actual value is not equal to the given one.

      Example:

       // assertions will pass:
       assertThat(1).isNotEqualTo(2);
       assertThat(1).isNotEqualTo(-1);
      
       // assertion will fail:
       assertThat(1).isNotEqualTo(1);
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is equal to the given one.
    • isZero

      public SELF isZero()
      Verifies that the actual value is equal to zero.

      Example:

       // assertions will pass
       assertThat(0).isZero();
       assertThat(0.0).isZero();
      
       // assertions will fail
       assertThat(42).isZero();
       assertThat(3.142).isZero();
      Specified by:
      isZero in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.
    • isNotZero

      public SELF isNotZero()
      Verifies that the actual value is not equal to zero.

      Example:

       // assertions will pass
       assertThat(42).isNotZero();
       assertThat(3.142).isNotZero();
      
       // assertions will fail
       assertThat(0).isNotZero();
       assertThat(0.0).isNotZero();
      Specified by:
      isNotZero in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.
    • isOne

      public SELF isOne()
      Verifies that the actual value is equal to one.

      Example:

       // assertions will pass
       assertThat(1).isOne();
       assertThat(1.0).isOne();
      
       // assertions will fail
       assertThat(42).isOne();
       assertThat(3.142).isOne();
      Specified by:
      isOne in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.
    • isPositive

      public SELF isPositive()
      Verifies that the actual value is positive.

      Example:

       // assertions will pass
       assertThat(42).isPositive();
       assertThat(3.142).isPositive();
      
       // assertions will fail
       assertThat(0).isPositive();
       assertThat(-42).isPositive();
      Specified by:
      isPositive in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.
    • isNegative

      public SELF isNegative()
      Verifies that the actual value is negative.

      Example:

       // assertions will pass
       assertThat(-42).isNegative();
       assertThat(-3.124).isNegative();
      
       // assertions will fail
       assertThat(0).isNegative();
       assertThat(42).isNegative();
      Specified by:
      isNegative in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.
    • isNotNegative

      public SELF isNotNegative()
      Verifies that the actual value is non negative (positive or equal zero).

      Example:

       // assertions will pass
       assertThat(42).isNotNegative();
       assertThat(0).isNotNegative();
      
       // assertions will fail
       assertThat(-42).isNotNegative();
       assertThat(-3.124).isNotNegative();
      Specified by:
      isNotNegative in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.
    • isNotPositive

      public SELF isNotPositive()
      Verifies that the actual value is non positive (negative or equal zero).

      Example:

       // assertions will pass
       assertThat(-42).isNotPositive();
       assertThat(0).isNotPositive();
      
       // assertions will fail
       assertThat(42).isNotPositive();
       assertThat(3.124).isNotPositive();
      Specified by:
      isNotPositive in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.
    • isEven

      public SELF isEven()
      Verifies that the actual value is even.

      Example:

       // assertions will pass
       assertThat(12).isEven();
       assertThat(-46).isEven();
      
       // assertions will fail
       assertThat(3).isEven();
       assertThat(15).isEven();
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is not even.
      Since:
      3.17.0
    • isOdd

      public SELF isOdd()
      Verifies that the actual value is odd.

      Example:

       // assertions will pass
       assertThat(3).isOdd();
       assertThat(-17).isOdd();
      
       // assertions will fail
       assertThat(2).isOdd();
       assertThat(-24).isOdd();
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is not odd.
      Since:
      3.17.0
    • isLessThan

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

      Example:

       // assertions will pass:
       assertThat(1).isLessThan(2);
       assertThat(-2).isLessThan(-1);
      
       // assertions will fail:
       assertThat(1).isLessThan(0);
       assertThat(1).isLessThan(1);
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is equal to or greater than the given one.
    • isLessThanOrEqualTo

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

      Example:

       // assertions will pass:
       assertThat(1).isLessThanOrEqualTo(2);
       assertThat(-1).isLessThanOrEqualTo(-2);
       assertThat(1).isLessThanOrEqualTo(1);
      
       // assertions will fail:
       assertThat(1).isLessThanOrEqualTo(2);
       assertThat(-1).isLessThanOrEqualTo(-2);
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is greater than the given one.
    • isGreaterThan

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

      Example:

       // assertions will pass:
       assertThat(1).isGreaterThan(0);
       assertThat(-1).isGreaterThan(-2);
      
       // assertions will fail:
       assertThat(1).isGreaterThan(2);
       assertThat(1).isGreaterThan(1);
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is equal to or less than the given one.
    • isGreaterThanOrEqualTo

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

      Example:

       // assertions will pass:
       assertThat(2).isGreaterThanOrEqualTo(1);
       assertThat(1).isGreaterThanOrEqualTo(1);
      
       // assertions will fail:
       assertThat(1).isGreaterThanOrEqualTo(2);
       assertThat(-1).isGreaterThanOrEqualTo(1);
      Parameters:
      other - the given value to compare the actual value to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual value is null.
      AssertionError - if the actual value is less than the given one.
    • isBetween

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

      Example:

       // assertions will pass
       assertThat(1).isBetween(-1, 2);
       assertThat(1).isBetween(1, 2);
       assertThat(1).isBetween(0, 1);
      
       // assertion will fail
       assertThat(1).isBetween(2, 3);
      Specified by:
      isBetween in interface ComparableAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Specified by:
      isBetween in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Overrides:
      isBetween in class AbstractComparableAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Parameters:
      start - the start value (inclusive), expected not to be null.
      end - the end value (inclusive), expected not to be null.
      Returns:
      this assertion object.
    • isStrictlyBetween

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

      Example:

       // assertion will pass
       assertThat(1).isStrictlyBetween(-1, 2);
      
       // assertions will fail
       assertThat(1).isStrictlyBetween(1, 2);
       assertThat(1).isStrictlyBetween(0, 1);
       assertThat(1).isStrictlyBetween(2, 3);
      Specified by:
      isStrictlyBetween in interface ComparableAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Specified by:
      isStrictlyBetween in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Overrides:
      isStrictlyBetween in class AbstractComparableAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Parameters:
      start - the start value (exclusive), expected not to be null.
      end - the end value (exclusive), expected not to be null.
      Returns:
      this assertion object.
    • isCloseTo

      public SELF isCloseTo(int expected, Offset<Integer> offset)
      Verifies that the actual number is close to the given one within the given offset value.

      When abs(actual - expected) == offset value, the assertion:

      Breaking change since 2.9.0/3.9.0: using Assertions.byLessThan(Integer) implies a strict comparison, use Assertions.within(Integer) to get the old behavior.

      Examples:

       // assertions will pass:
       assertThat(5).isCloseTo(7, within(3));
       assertThat(5).isCloseTo(7, byLessThan(3));
      
       // if difference is exactly equals to the offset, it's ok ...
       assertThat(5).isCloseTo(7, within(2));
       // ... but not with byLessThan which implies a strict comparison
       assertThat(5).isCloseTo(7, byLessThan(2)); // FAIL
      
       // assertions will fail
       assertThat(5).isCloseTo(7, within(1));
       assertThat(5).isCloseTo(7, byLessThan(1));
       assertThat(5).isCloseTo(7, byLessThan(2));
      Parameters:
      expected - the given int to compare the actual value to.
      offset - the given positive offset.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given offset is null.
      AssertionError - if the actual value is not close enough to the given one.
    • isNotCloseTo

      public SELF isNotCloseTo(int expected, Offset<Integer> offset)
      Verifies that the actual number is not close to the given one by less than the given offset.

      When abs(actual - expected) == offset value, the assertion:

      Breaking change since 2.9.0/3.9.0: using Assertions.byLessThan(Integer) implies a strict comparison, use Assertions.within(Integer) to get the old behavior.

      Examples:

       // assertions will pass:
       assertThat(5).isNotCloseTo(7, byLessThan(1));
       assertThat(5).isNotCloseTo(7, within(1));
       // diff == offset but isNotCloseTo succeeds as we use byLessThan
       assertThat(5).isNotCloseTo(7, byLessThan(2));
      
       // assertions will fail
       assertThat(5).isNotCloseTo(7, within(2));
       assertThat(5).isNotCloseTo(7, byLessThan(3));
      Parameters:
      expected - the given int to compare the actual value to.
      offset - the given positive offset.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given offset is null.
      AssertionError - if the actual value is close to the given one.
      Since:
      2.6.0 / 3.6.0
      See Also:
    • isCloseTo

      public SELF isCloseTo(Integer expected, Offset<Integer> offset)
      Verifies that the actual number is close to the given one within the given offset value.

      When abs(actual - expected) == offset value, the assertion:

      Breaking change since 2.9.0/3.9.0: using Assertions.byLessThan(Integer) implies a strict comparison, use Assertions.within(Integer) to get the old behavior.

      Examples:

       // assertions succeed:
       assertThat(5).isCloseTo(7, within(3));
       assertThat(5).isCloseTo(7, byLessThan(3));
      
       // if difference is exactly equals to the offset, it's ok ...
       assertThat(5).isCloseTo(7, within(2));
       // ... but not with byLessThan which implies a strict comparison
       assertThat(5).isCloseTo(7, byLessThan(2)); // FAIL
      
       // assertions fail
       assertThat(5).isCloseTo(7, within(1));
       assertThat(5).isCloseTo(7, byLessThan(1));
       assertThat(5).isCloseTo(7, byLessThan(2));
      Specified by:
      isCloseTo in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Parameters:
      expected - the given int to compare the actual value to.
      offset - the given positive offset.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given offset is null.
      AssertionError - if the actual value is not close enough to the given one.
    • isNotCloseTo

      public SELF isNotCloseTo(Integer expected, Offset<Integer> offset)
      Verifies that the actual number is not close to the given one by less than the given offset.

      When abs(actual - expected) == offset value, the assertion:

      Breaking change since 2.9.0/3.9.0: using Assertions.byLessThan(Integer) implies a strict comparison, use Assertions.within(Integer) to get the old behavior.

      Examples:

       // assertions will pass:
       assertThat(5).isNotCloseTo(7, byLessThan(1));
       assertThat(5).isNotCloseTo(7, within(1));
       // diff == offset but isNotCloseTo succeeds as we use byLessThan
       assertThat(5).isNotCloseTo(7, byLessThan(2));
      
       // assertions will fail
       assertThat(5).isNotCloseTo(7, within(2));
       assertThat(5).isNotCloseTo(7, byLessThan(3));
      Specified by:
      isNotCloseTo in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Parameters:
      expected - the given int to compare the actual value to.
      offset - the given positive offset.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given offset is null.
      AssertionError - if the actual value is close to the given one.
      Since:
      2.6.0 / 3.6.0
      See Also:
    • isCloseTo

      public SELF isCloseTo(Integer expected, Percentage percentage)
      Verifies that the actual number is close to the given one within the given percentage.
      If difference is equal to the percentage value, assertion is considered valid.

      Example with integer:

       // assertions will pass:
       assertThat(11).isCloseTo(Integer.valueOf(10), withinPercentage(20));
      
       // if difference is exactly equals to the computed offset (1), it's ok
       assertThat(11).isCloseTo(Integer.valueOf(10), withinPercentage(10));
      
       // assertion will fail
       assertThat(11).isCloseTo(Integer.valueOf(10), withinPercentage(5));
      Specified by:
      isCloseTo in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Parameters:
      expected - the given number to compare the actual value to.
      percentage - the given positive percentage.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given offset is null.
      NullPointerException - if the expected number is null.
      AssertionError - if the actual value is not equal to the given one.
    • isNotCloseTo

      public SELF isNotCloseTo(Integer expected, Percentage percentage)
      Verifies that the actual number is not close to the given one by the given percentage.
      If difference is equal to the percentage value, the assertion fails.

      Example with integer:

       // assertion will pass:
       assertThat(11).isNotCloseTo(Integer.valueOf(10), withinPercentage(5));
      
       // assertions will fail
       assertThat(11).isNotCloseTo(Integer.valueOf(10), withinPercentage(10));
       assertThat(11).isNotCloseTo(Integer.valueOf(10), withinPercentage(20));
      Specified by:
      isNotCloseTo in interface NumberAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Parameters:
      expected - the given number to compare the actual value to.
      percentage - the given positive percentage.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given offset is null.
      NullPointerException - if the expected number is null.
      AssertionError - if the actual value is close to the given one.
      Since:
      2.6.0 / 3.6.0
    • isCloseTo

      public SELF isCloseTo(int expected, Percentage percentage)
      Verifies that the actual number is close to the given one within the given percentage.
      If difference is equal to the percentage value, assertion is considered valid.

      Example with integer:

       // assertions will pass:
       assertThat(11).isCloseTo(10, withinPercentage(20));
      
       // if difference is exactly equals to the computed offset (1), it's ok
       assertThat(11).isCloseTo(10, withinPercentage(10));
      
       // assertion will fail
       assertThat(11).isCloseTo(10, withinPercentage(5));
      Parameters:
      expected - the given number to compare the actual value to.
      percentage - the given positive percentage.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given offset is null.
      NullPointerException - if the expected number is null.
      AssertionError - if the actual value is not close enough to the given one.
    • isNotCloseTo

      public SELF isNotCloseTo(int expected, Percentage percentage)
      Verifies that the actual number is not close to the given one by the given percentage.
      If difference is equal to the percentage value, the assertion fails.

      Example with integer:

       // assertion will pass:
       assertThat(11).isNotCloseTo(10, withinPercentage(5));
      
       // assertions will fail
       assertThat(11).isNotCloseTo(10, withinPercentage(10));
       assertThat(11).isNotCloseTo(10, withinPercentage(20));
      Parameters:
      expected - the given number to compare the actual value to.
      percentage - the given positive percentage.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given Percentage is null.
      NullPointerException - if the expected number is null.
      AssertionError - if the actual value is close to the given one.
      Since:
      2.6.0 / 3.6.0
    • usingComparator

      public SELF usingComparator(Comparator<? super Integer> 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 AbstractIntegerAssert<SELF>,Integer>
      Overrides:
      usingComparator in class AbstractComparableAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Parameters:
      customComparator - the comparator to use for the incoming assertion checks.
      Returns:
      this assertion object.
    • usingComparator

      public SELF usingComparator(Comparator<? super Integer> 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 AbstractIntegerAssert<SELF>,Integer>
      Overrides:
      usingComparator in class AbstractComparableAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      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 AbstractIntegerAssert<SELF>,Integer>
      Overrides:
      usingDefaultComparator in class AbstractComparableAssert<SELF extends AbstractIntegerAssert<SELF>,Integer>
      Returns:
      this assertion object.