Class AbstractLocalDateTimeAssert<SELF extends AbstractLocalDateTimeAssert<SELF>>

    • Field Detail

      • NULL_LOCAL_DATE_TIME_PARAMETER_MESSAGE

        public static final String NULL_LOCAL_DATE_TIME_PARAMETER_MESSAGE
        See Also:
        Constant Field Values
    • Constructor Detail

      • AbstractLocalDateTimeAssert

        protected AbstractLocalDateTimeAssert​(LocalDateTime actual,
                                              Class<?> selfType)
        Parameters:
        selfType - the "self type"
        actual - the actual value to verify
    • Method Detail

      • isCloseToUtcNow

        public SELF isCloseToUtcNow​(TemporalUnitOffset offset)
        Verifies that the actual LocalDateTime is close to the current date and time on the UTC timezone, according to the given TemporalUnitOffset. You can build the offset parameter using Assertions.within(long, TemporalUnit) or Assertions.byLessThan(long, TemporalUnit).

        If the difference is equal to the offset, the assertion succeeds.

        Example:

         LocalDateTime actual = LocalDateTime.now(Clock.systemUTC());
        
         // assertion will pass as if executed less than one second after actual was built
         assertThat(actual).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));
        
         // assertion will fail
         assertThat(actual.plusSeconds(2)).isCloseToUtcNow(within(1, ChronoUnit.SECONDS));
        Parameters:
        offset - The offset used for comparison
        Returns:
        this assertion object
        Throws:
        NullPointerException - if offset parameter is null.
        AssertionError - if the actual LocalDateTime is null.
        AssertionError - if the actual LocalDateTime is not close to the current time by less than the given offset.
      • buildDefaultComparables

        private Comparables buildDefaultComparables()
      • convertToLocalDateTimeArray

        private static Object[] convertToLocalDateTimeArray​(String... dateTimesAsString)
      • checkIsNotNullAndNotEmpty

        private void checkIsNotNullAndNotEmpty​(Object[] values)
      • assertLocalDateTimeAsStringParameterIsNotNull

        private static void assertLocalDateTimeAsStringParameterIsNotNull​(String localDateTimeAsString)
        Check that the LocalDateTime string representation to compare actual LocalDateTime to is not null, otherwise throws a IllegalArgumentException with an explicit message
        Parameters:
        localDateTimeAsString - String representing the LocalDateTime to compare actual with
        Throws:
        IllegalArgumentException - with an explicit message if the given String is null
      • isEqualToIgnoringNanos

        public SELF isEqualToIgnoringNanos​(LocalDateTime other)
        Verifies that actual and given LocalDateTime have same year, month, day, hour, minute and second fields, (nanosecond fields are ignored in comparison).

        Assertion can fail with localDateTimes in same chronological nanosecond time window, e.g :

        2000-01-01T00:00:01.000000000 and 2000-01-01T00:00:00.999999999.

        Assertion fails as second fields differ even if time difference is only 1ns.

        Code example :

         // successful assertions
         LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 0, 0, 1, 0);
         LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 0, 0, 1, 456);
         assertThat(localDateTime1).isEqualToIgnoringNanos(localDateTime2);
        
         // failing assertions (even if time difference is only 1ms)
         LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 1, 0, 0, 1, 0);
         LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 0, 0, 0, 999999999);
         assertThat(localDateTimeA).isEqualToIgnoringNanos(localDateTimeB);
        Parameters:
        other - the given LocalDateTime.
        Returns:
        this assertion object.
        Throws:
        AssertionError - if the actual LocalDateTime is null.
        IllegalArgumentException - if other LocalDateTime is null.
        AssertionError - if the actual LocalDateTime is are not equal with nanoseconds ignored.
      • isEqualToIgnoringSeconds

        public SELF isEqualToIgnoringSeconds​(LocalDateTime other)
        Verifies that actual and given LocalDateTime have same year, month, day, hour and minute fields (second and nanosecond fields are ignored in comparison).

        Assertion can fail with LocalDateTimes in same chronological second time window, e.g :

        2000-01-01T00:01:00.000 and 2000-01-01T00:00:59.000.

        Assertion fails as minute fields differ even if time difference is only 1s.

        Code example :

         // successful assertions
         LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 23, 50, 0, 0);
         LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 23, 50, 10, 456);
         assertThat(localDateTime1).isEqualToIgnoringSeconds(localDateTime2);
        
         // failing assertions (even if time difference is only 1ms)
         LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 1, 23, 50, 00, 000);
         LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 23, 49, 59, 999);
         assertThat(localDateTimeA).isEqualToIgnoringSeconds(localDateTimeB);
        Parameters:
        other - the given LocalDateTime.
        Returns:
        this assertion object.
        Throws:
        AssertionError - if the actual LocalDateTime is null.
        IllegalArgumentException - if other LocalDateTime is null.
        AssertionError - if the actual LocalDateTime is are not equal with second and nanosecond fields ignored.
      • isEqualToIgnoringMinutes

        public SELF isEqualToIgnoringMinutes​(LocalDateTime other)
        Verifies that actual and given LocalDateTime have same year, month, day and hour fields (minute, second and nanosecond fields are ignored in comparison).

        Assertion can fail with localDateTimes in same chronological second time window, e.g :

        2000-01-01T01:00:00.000 and 2000-01-01T00:59:59.000.

        Time difference is only 1s but hour fields differ.

        Code example :

         // successful assertions
         LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 23, 50, 0, 0);
         LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 23, 00, 2, 7);
         assertThat(localDateTime1).isEqualToIgnoringMinutes(localDateTime2);
        
         // failing assertions (even if time difference is only 1ms)
         LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 1, 01, 00, 00, 000);
         LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 00, 59, 59, 999);
         assertThat(localDateTimeA).isEqualToIgnoringMinutes(localDateTimeB);
        Parameters:
        other - the given LocalDateTime.
        Returns:
        this assertion object.
        Throws:
        AssertionError - if the actual LocalDateTime is null.
        IllegalArgumentException - if other LocalDateTime is null.
        AssertionError - if the actual LocalDateTime is are not equal ignoring minute, second and nanosecond fields.
      • isEqualToIgnoringHours

        public SELF isEqualToIgnoringHours​(LocalDateTime other)
        Verifies that actual and given LocalDateTime have same year, month and day fields (hour, minute, second and nanosecond fields are ignored in comparison).

        Assertion can fail with localDateTimes in same chronological minute time window, e.g :

        2000-01-01T23:59:00.000 and 2000-01-02T00:00:00.000.

        Time difference is only 1min but day fields differ.

        Code example :

         // successful assertions
         LocalDateTime localDateTime1 = LocalDateTime.of(2000, 1, 1, 23, 59, 59, 999);
         LocalDateTime localDateTime2 = LocalDateTime.of(2000, 1, 1, 00, 00, 00, 000);
         assertThat(localDateTime1).isEqualToIgnoringHours(localDateTime2);
        
         // failing assertions (even if time difference is only 1ms)
         LocalDateTime localDateTimeA = LocalDateTime.of(2000, 1, 2, 00, 00, 00, 000);
         LocalDateTime localDateTimeB = LocalDateTime.of(2000, 1, 1, 23, 59, 59, 999);
         assertThat(localDateTimeA).isEqualToIgnoringHours(localDateTimeB);
        Parameters:
        other - the given LocalDateTime.
        Returns:
        this assertion object.
        Throws:
        AssertionError - if the actual LocalDateTime is null.
        IllegalArgumentException - if other LocalDateTime is null.
        AssertionError - if the actual LocalDateTime is are not equal with second and nanosecond fields ignored.
      • isBetween

        public SELF isBetween​(LocalDateTime startInclusive,
                              LocalDateTime endInclusive)
        Verifies that the actual LocalDateTime is in the [start, end] period (start and end included) according to the ChronoLocalDateTime.timeLineOrder() comparator.

        ChronoLocalDateTime.timeLineOrder() compares LocalDateTime in time-line order ignoring the chronology, this is equivalent to comparing the epoch-day and nano-of-day.

        This behaviour can be overridden by AbstractTemporalAssert.usingComparator(Comparator).

        Example:

         LocalDateTime localDateTime = LocalDateTime.now();
        
         // assertions succeed:
         assertThat(localDateTime).isBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1))
                                  .isBetween(localDateTime, localDateTime.plusSeconds(1))
                                  .isBetween(localDateTime.minusSeconds(1), localDateTime)
                                  .isBetween(localDateTime, localDateTime);
        
         // assertions fail:
         assertThat(localDateTime).isBetween(localDateTime.minusSeconds(10), localDateTime.minusSeconds(1));
         assertThat(localDateTime).isBetween(localDateTime.plusSeconds(1), localDateTime.plusSeconds(10));
        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:
        AssertionError - if the actual value is null.
        NullPointerException - if start value is null.
        NullPointerException - if end value is null.
        AssertionError - if the actual value is not in [start, end] period.
        Since:
        3.7.1
      • isBetween

        public SELF isBetween​(String startInclusive,
                              String endInclusive)
        Same assertion as isBetween(LocalDateTime, LocalDateTime) but here you pass LocalDateTime String representations which must follow ISO LocalDateTime format to allow calling LocalDateTime.parse(CharSequence) method.

        Example:

         LocalDateTime firstOfJanuary2000 = LocalDateTime.parse("2000-01-01T00:00:00");
        
         // assertions succeed:
         assertThat(firstOfJanuary2000).isBetween("1999-12-31T23:59:59", "2000-01-01T00:00:01")
                                       .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:01")
                                       .isBetween("1999-12-31T23:59:59", "2000-01-01T00:00:00")
                                       .isBetween("2000-01-01T00:00:00", "2000-01-01T00:00:00");
        
         // assertion fails:
         assertThat(firstOfJanuary2000).isBetween("1999-01-01T00:00:01", "1999-12-31T23:59:59");
        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:
        AssertionError - if the actual value is null.
        NullPointerException - if start value is null.
        NullPointerException - if end value is null.
        DateTimeParseException - if any of the given String can't be converted to a LocalDateTime.
        AssertionError - if the actual value is not in [start, end] period.
        Since:
        3.7.1
      • isStrictlyBetween

        public SELF isStrictlyBetween​(LocalDateTime startExclusive,
                                      LocalDateTime endExclusive)
        Verifies that the actual LocalDateTime is in the ]start, end[ period (start and end excluded) according to the ChronoLocalDateTime.timeLineOrder() comparator.

        ChronoLocalDateTime.timeLineOrder() compares LocalDateTime in time-line order ignoring the chronology, this is equivalent to comparing the epoch-day and nano-of-day.

        This behaviour can be overridden by AbstractTemporalAssert.usingComparator(Comparator).

        Example:

         LocalDateTime localDateTime = LocalDateTime.now();
        
         // assertion succeeds:
         assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(1), localDateTime.plusSeconds(1));
        
         // assertions fail:
         assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(10), localDateTime.minusSeconds(1));
         assertThat(localDateTime).isStrictlyBetween(localDateTime.plusSeconds(1), localDateTime.plusSeconds(10));
         assertThat(localDateTime).isStrictlyBetween(localDateTime, localDateTime.plusSeconds(1));
         assertThat(localDateTime).isStrictlyBetween(localDateTime.minusSeconds(1), localDateTime);
        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:
        AssertionError - if the actual value is null.
        NullPointerException - if start value is null.
        NullPointerException - if end value is null.
        AssertionError - if the actual value is not in ]start, end[ period.
        Since:
        3.7.1
      • isStrictlyBetween

        public SELF isStrictlyBetween​(String startExclusive,
                                      String endExclusive)
        Same assertion as isStrictlyBetween(LocalDateTime, LocalDateTime) but here you pass LocalDateTime String representations which must follow ISO LocalDateTime format to allow calling LocalDateTime.parse(CharSequence) method.

        Example:

         LocalDateTime firstOfJanuary2000 = LocalDateTime.parse("2000-01-01T00:00:00");
        
         // assertion succeeds:
         assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59", "2000-01-01T00:00:01");
        
         // assertions fail:
         assertThat(firstOfJanuary2000).isStrictlyBetween("1999-01-01T00:00:01", "1999-12-31T23:59:59");
         assertThat(firstOfJanuary2000).isStrictlyBetween("2000-01-01T00:00:00", "2000-01-01T00:00:01");
         assertThat(firstOfJanuary2000).isStrictlyBetween("1999-12-31T23:59:59", "2000-01-01T00:00:00");
        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:
        AssertionError - if the actual value is null.
        NullPointerException - if start value is null.
        NullPointerException - if end value is null.
        DateTimeParseException - if any of the given String can't be converted to a LocalDateTime.
        AssertionError - if the actual value is not in ]start, end[ period.
        Since:
        3.7.1
      • areEqualIgnoringNanos

        private static boolean areEqualIgnoringNanos​(LocalDateTime actual,
                                                     LocalDateTime other)
        Returns true if both datetime are in the same year, month and day of month, hour, minute and second, false otherwise.
        Parameters:
        actual - the actual datetime. expected not be null
        other - the other datetime. expected not be null
        Returns:
        true if both datetime are in the same year, month and day of month, hour, minute and second, false otherwise.
      • areEqualIgnoringSeconds

        private static boolean areEqualIgnoringSeconds​(LocalDateTime actual,
                                                       LocalDateTime other)
        Returns true if both datetime are in the same year, month, day of month, hour and minute, false otherwise.
        Parameters:
        actual - the actual datetime. expected not be null
        other - the other datetime. expected not be null
        Returns:
        true if both datetime are in the same year, month, day of month, hour and minute, false otherwise.
      • areEqualIgnoringMinutes

        private static boolean areEqualIgnoringMinutes​(LocalDateTime actual,
                                                       LocalDateTime other)
        Returns true if both datetime are in the same year, month, day of month and hour, false otherwise.
        Parameters:
        actual - the actual datetime. expected not be null
        other - the other datetime. expected not be null
        Returns:
        true if both datetime are in the same year, month, day of month and hour, false otherwise.
      • haveSameYearMonthAndDayOfMonth

        private static boolean haveSameYearMonthAndDayOfMonth​(LocalDateTime actual,
                                                              LocalDateTime other)
        Returns true if both datetime are in the same year, month and day of month, false otherwise.
        Parameters:
        actual - the actual datetime. expected not be null
        other - the other datetime. expected not be null
        Returns:
        true if both datetime are in the same year, month and day of month, false otherwise
      • haveSameYearAndMonth

        private static boolean haveSameYearAndMonth​(LocalDateTime actual,
                                                    LocalDateTime other)
        Returns true if both datetime are in the same year and month, false otherwise.
        Parameters:
        actual - the actual datetime. expected not be null
        other - the other datetime. expected not be null
        Returns:
        true if both datetime are in the same year and month, false otherwise
      • haveSameYear

        private static boolean haveSameYear​(LocalDateTime actual,
                                            LocalDateTime other)
        Returns true if both datetime are in the same year, false otherwise.
        Parameters:
        actual - the actual datetime. expected not be null
        other - the other datetime. expected not be null
        Returns:
        true if both datetime are in the same year, false otherwise