Class AbstractTemporalAssert<SELF extends AbstractTemporalAssert<SELF,TEMPORAL>,TEMPORAL extends Temporal>

java.lang.Object
org.assertj.core.api.AbstractAssert<SELF,TEMPORAL>
org.assertj.core.api.AbstractTemporalAssert<SELF,TEMPORAL>
All Implemented Interfaces:
Assert<SELF,TEMPORAL>, Descriptable<SELF>, ExtensionPoints<SELF,TEMPORAL>
Direct Known Subclasses:
AbstractInstantAssert, AbstractLocalDateAssert, AbstractLocalDateTimeAssert, AbstractLocalTimeAssert, AbstractOffsetDateTimeAssert, AbstractOffsetTimeAssert, AbstractZonedDateTimeAssert

public abstract class AbstractTemporalAssert<SELF extends AbstractTemporalAssert<SELF,TEMPORAL>,TEMPORAL extends Temporal> extends AbstractAssert<SELF,TEMPORAL>
Base class for all implementations of assertions for Temporals.
Since:
3.7.0
  • Constructor Details

    • AbstractTemporalAssert

      protected AbstractTemporalAssert(TEMPORAL actual, Class<?> selfType)
      Creates a new AbstractTemporalAssert.
      Parameters:
      selfType - the "self type"
      actual - the actual value to verify
  • Method Details

    • getActual

      protected TEMPORAL getActual()
    • isCloseTo

      public SELF isCloseTo(TEMPORAL other, TemporalOffset<? super TEMPORAL> offset)
      Verifies that the actual Temporal is close to the other according to the given TemporalOffset.

      You can build the offset parameter using Assertions.within(long, TemporalUnit) or Assertions.byLessThan(long, TemporalUnit).

      Example:

       LocalTime _07_10 = LocalTime.of(7, 10);
       LocalTime _07_42 = LocalTime.of(7, 42);
      
       // assertions will pass
       assertThat(_07_10).isCloseTo(_07_42, within(1, ChronoUnit.HOURS));
       assertThat(_07_10).isCloseTo(_07_42, within(32, ChronoUnit.MINUTES));
      
       // assertions will fail
       assertThat(_07_10).isCloseTo(_07_42, byLessThan(32, ChronoUnit.MINUTES));
       assertThat(_07_10).isCloseTo(_07_42, within(10, ChronoUnit.SECONDS));
      Parameters:
      other - the temporal to compare actual to
      offset - the offset used for comparison
      Returns:
      this assertion object
      Throws:
      NullPointerException - if Temporal or TemporalOffset parameter is null.
      AssertionError - if the actual Temporal is null.
      AssertionError - if the actual Temporal is not close to the given for a provided offset.
    • isCloseTo

      public SELF isCloseTo(String otherAsString, TemporalOffset<? super TEMPORAL> offset)
      Same assertion as isCloseTo(Temporal, TemporalOffset) but the TEMPORAL is built from a given String that follows predefined ISO date format Predefined Formatters to allow calling parse(String))} method.

      Example :

       assertThat(LocalTime.parse("07:10:30")).isCloseTo("07:12:11", within(5, ChronoUnit.MINUTES));
      Parameters:
      otherAsString - String representing a TEMPORAL.
      offset - the offset used for comparison
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual Temporal is null.
      NullPointerException - if temporal string representation or TemporalOffset parameter is null.
      AssertionError - if the actual Temporal is null.
      AssertionError - if the actual Temporal is not close to the given for a provided offset.
    • parse

      protected abstract TEMPORAL parse(String temporalAsString)
      Obtains an instance of TEMPORAL from a string representation in ISO date format.
      Parameters:
      temporalAsString - the string to parse, not null
      Returns:
      the parsed TEMPORAL, not null
    • usingComparator

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

      public SELF usingComparator(Comparator<? super TEMPORAL> customComparator, 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 interface Assert<SELF extends AbstractTemporalAssert<SELF,TEMPORAL>,TEMPORAL extends Temporal>
      Overrides:
      usingComparator in class AbstractAssert<SELF extends AbstractTemporalAssert<SELF,TEMPORAL>,TEMPORAL extends Temporal>
      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 interface Assert<SELF extends AbstractTemporalAssert<SELF,TEMPORAL>,TEMPORAL extends Temporal>
      Overrides:
      usingDefaultComparator in class AbstractAssert<SELF extends AbstractTemporalAssert<SELF,TEMPORAL>,TEMPORAL extends Temporal>
      Returns:
      this assertion object.