Fluent assertion library providing rich assertions for Java tests with expressive failure messages
—
Comprehensive assertions for both legacy Date API and modern java.time API including temporal comparisons, offsets, and period validations.
import static org.assertj.core.api.Assertions.*;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;Assertions for java.util.Date objects.
DateAssert assertThat(Date actual)
// Date comparison methods
DateAssert isBefore(Date date)
DateAssert isBeforeOrEqualTo(Date date)
DateAssert isAfter(Date date)
DateAssert isAfterOrEqualTo(Date date)
DateAssert isEqualTo(Date expected)
DateAssert isNotEqualTo(Date expected)
DateAssert isEqualToIgnoringHours(Date expected)
DateAssert isEqualToIgnoringMinutes(Date expected)
DateAssert isEqualToIgnoringSeconds(Date expected)
DateAssert isEqualToIgnoringMillis(Date expected)
// Date range methods
DateAssert isBetween(Date start, Date end)
DateAssert isStrictlyBetween(Date start, Date end)
DateAssert isCloseTo(Date expected, long deltaInMilliseconds)
// Date component methods
DateAssert isInSameYearAs(Date date)
DateAssert isInSameMonthAs(Date date)
DateAssert isInSameDayAs(Date date)
DateAssert isInSameHourAs(Date date)
DateAssert isInSameMinuteAs(Date date)
DateAssert isInSameSecondAs(Date date)
// Date specific checks
DateAssert isToday()
DateAssert isInThePast()
DateAssert isInTheFuture()Usage examples:
Date now = new Date();
Date yesterday = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Date tomorrow = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
assertThat(now)
.isToday()
.isAfter(yesterday)
.isBefore(tomorrow)
.isInSameDayAs(now);
assertThat(yesterday).isInThePast();
assertThat(tomorrow).isInTheFuture();Assertions for LocalDate objects.
LocalDateAssert assertThat(LocalDate actual)
// LocalDate comparison methods
LocalDateAssert isBefore(LocalDate date)
LocalDateAssert isBeforeOrEqualTo(LocalDate date)
LocalDateAssert isAfter(LocalDate date)
LocalDateAssert isAfterOrEqualTo(LocalDate date)
LocalDateAssert isEqualTo(LocalDate expected)
LocalDateAssert isNotEqualTo(LocalDate expected)
// LocalDate range methods
LocalDateAssert isBetween(LocalDate start, LocalDate end)
LocalDateAssert isStrictlyBetween(LocalDate start, LocalDate end)
LocalDateAssert isCloseTo(LocalDate expected, TemporalUnitWithinOffset offset)
// LocalDate component methods
LocalDateAssert hasYear(int year)
LocalDateAssert hasMonth(Month month)
LocalDateAssert hasMonthValue(int month)
LocalDateAssert hasDayOfMonth(int dayOfMonth)
LocalDateAssert hasDayOfWeek(DayOfWeek dayOfWeek)
LocalDateAssert hasDayOfYear(int dayOfYear)
// LocalDate specific checks
LocalDateAssert isToday()
LocalDateAssert isYesterday()
LocalDateAssert isTomorrow()
LocalDateAssert isInThePast()
LocalDateAssert isInTheFuture()
LocalDateAssert isWeekend()
LocalDateAssert isNotWeekend()
LocalDateAssert isLeapYear()
LocalDateAssert isNotLeapYear()Usage examples:
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1990, Month.JUNE, 15);
LocalDate futureDate = LocalDate.of(2025, 12, 31);
assertThat(today)
.isToday()
.isAfter(birthday)
.isBefore(futureDate);
assertThat(birthday)
.hasYear(1990)
.hasMonth(Month.JUNE)
.hasDayOfMonth(15)
.isInThePast();
LocalDate weekend = LocalDate.of(2023, 12, 23); // Saturday
assertThat(weekend).isWeekend();Assertions for LocalDateTime objects combining date and time.
LocalDateTimeAssert assertThat(LocalDateTime actual)
// LocalDateTime comparison methods
LocalDateTimeAssert isBefore(LocalDateTime dateTime)
LocalDateTimeAssert isBeforeOrEqualTo(LocalDateTime dateTime)
LocalDateTimeAssert isAfter(LocalDateTime dateTime)
LocalDateTimeAssert isAfterOrEqualTo(LocalDateTime dateTime)
LocalDateTimeAssert isEqualTo(LocalDateTime expected)
LocalDateTimeAssert isEqualToIgnoringNanos(LocalDateTime expected)
LocalDateTimeAssert isEqualToIgnoringSeconds(LocalDateTime expected)
LocalDateTimeAssert isEqualToIgnoringMinutes(LocalDateTime expected)
LocalDateTimeAssert isEqualToIgnoringHours(LocalDateTime expected)
// LocalDateTime range methods
LocalDateTimeAssert isBetween(LocalDateTime start, LocalDateTime end)
LocalDateTimeAssert isStrictlyBetween(LocalDateTime start, LocalDateTime end)
LocalDateTimeAssert isCloseTo(LocalDateTime expected, TemporalUnitWithinOffset offset)
// LocalDateTime component methods (combines LocalDate and LocalTime)
LocalDateTimeAssert hasYear(int year)
LocalDateTimeAssert hasHour(int hour)
LocalDateTimeAssert hasMinute(int minute)
LocalDateTimeAssert hasSecond(int second)
LocalDateTimeAssert hasNano(int nano)Usage examples:
LocalDateTime now = LocalDateTime.now();
LocalDateTime meeting = LocalDateTime.of(2023, 12, 25, 14, 30);
LocalDateTime deadline = now.plusHours(2);
assertThat(now)
.isBefore(deadline)
.isCloseTo(now, within(1, ChronoUnit.SECONDS));
assertThat(meeting)
.hasYear(2023)
.hasHour(14)
.hasMinute(30)
.isEqualToIgnoringSeconds(LocalDateTime.of(2023, 12, 25, 14, 30, 45));Assertions for LocalTime objects.
LocalTimeAssert assertThat(LocalTime actual)
// LocalTime comparison methods
LocalTimeAssert isBefore(LocalTime time)
LocalTimeAssert isBeforeOrEqualTo(LocalTime time)
LocalTimeAssert isAfter(LocalTime time)
LocalTimeAssert isAfterOrEqualTo(LocalTime time)
LocalTimeAssert isEqualTo(LocalTime expected)
LocalTimeAssert isEqualToIgnoringNanos(LocalTime expected)
LocalTimeAssert isEqualToIgnoringSeconds(LocalTime expected)
// LocalTime range methods
LocalTimeAssert isBetween(LocalTime start, LocalTime end)
LocalTimeAssert isStrictlyBetween(LocalTime start, LocalTime end)
// LocalTime component methods
LocalTimeAssert hasHour(int hour)
LocalTimeAssert hasMinute(int minute)
LocalTimeAssert hasSecond(int second)
LocalTimeAssert hasNano(int nano)Usage examples:
LocalTime now = LocalTime.now();
LocalTime noon = LocalTime.of(12, 0);
LocalTime evening = LocalTime.of(18, 30, 45);
assertThat(evening)
.isAfter(noon)
.hasHour(18)
.hasMinute(30)
.hasSecond(45);
assertThat(now).isBetween(LocalTime.MIN, LocalTime.MAX);Assertions for Instant objects representing precise moments in time.
InstantAssert assertThat(Instant actual)
// Instant comparison methods
InstantAssert isBefore(Instant instant)
InstantAssert isBeforeOrEqualTo(Instant instant)
InstantAssert isAfter(Instant instant)
InstantAssert isAfterOrEqualTo(Instant instant)
InstantAssert isEqualTo(Instant expected)
// Instant range methods
InstantAssert isBetween(Instant start, Instant end)
InstantAssert isStrictlyBetween(Instant start, Instant end)
InstantAssert isCloseTo(Instant expected, TemporalUnitWithinOffset offset)
// Instant specific checks
InstantAssert isInThePast()
InstantAssert isInTheFuture()Usage examples:
Instant now = Instant.now();
Instant start = Instant.ofEpochSecond(1640995200); // 2022-01-01T00:00:00Z
Instant end = now.plusSeconds(3600);
assertThat(now)
.isAfter(start)
.isBefore(end)
.isCloseTo(now, within(1, ChronoUnit.SECONDS));Assertions for ZonedDateTime objects with timezone information.
ZonedDateTimeAssert assertThat(ZonedDateTime actual)
// ZonedDateTime comparison methods
ZonedDateTimeAssert isBefore(ZonedDateTime dateTime)
ZonedDateTimeAssert isAfter(ZonedDateTime dateTime)
ZonedDateTimeAssert isEqualTo(ZonedDateTime expected)
ZonedDateTimeAssert isEqualToIgnoringNanos(ZonedDateTime expected)
ZonedDateTimeAssert isEqualToIgnoringSeconds(ZonedDateTime expected)
ZonedDateTimeAssert isEqualToIgnoringMinutes(ZonedDateTime expected)
ZonedDateTimeAssert isEqualToIgnoringHours(ZonedDateTime expected)
// ZonedDateTime range methods
ZonedDateTimeAssert isBetween(ZonedDateTime start, ZonedDateTime end)
ZonedDateTimeAssert isCloseTo(ZonedDateTime expected, TemporalUnitWithinOffset offset)
// Zone-specific methods
ZonedDateTimeAssert hasZoneId(ZoneId expected)
ZonedDateTimeAssert hasZoneSameInstantAs(ZonedDateTime expected)Usage examples:
ZonedDateTime utc = ZonedDateTime.now(ZoneId.of("UTC"));
ZonedDateTime pst = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
ZonedDateTime future = utc.plusHours(5);
assertThat(utc)
.hasZoneId(ZoneId.of("UTC"))
.isBefore(future);
assertThat(utc)
.hasZoneSameInstantAs(pst.withZoneSameInstant(ZoneId.of("UTC")));Assertions for offset-based date/time objects.
OffsetDateTimeAssert assertThat(OffsetDateTime actual)
OffsetTimeAssert assertThat(OffsetTime actual)
// OffsetDateTime methods
OffsetDateTimeAssert isBefore(OffsetDateTime dateTime)
OffsetDateTimeAssert isAfter(OffsetDateTime dateTime)
OffsetDateTimeAssert isEqualTo(OffsetDateTime expected)
OffsetDateTimeAssert hasOffset(ZoneOffset expected)
// OffsetTime methods
OffsetTimeAssert isBefore(OffsetTime time)
OffsetTimeAssert isAfter(OffsetTime time)
OffsetTimeAssert isEqualTo(OffsetTime expected)
OffsetTimeAssert hasOffset(ZoneOffset expected)Assertions for Duration objects representing time spans.
DurationAssert assertThat(Duration actual)
// Duration comparison methods
DurationAssert isEqualTo(Duration expected)
DurationAssert isNotEqualTo(Duration expected)
DurationAssert isGreaterThan(Duration expected)
DurationAssert isGreaterThanOrEqualTo(Duration expected)
DurationAssert isLessThan(Duration expected)
DurationAssert isLessThanOrEqualTo(Duration expected)
// Duration range methods
DurationAssert isBetween(Duration start, Duration end)
DurationAssert isStrictlyBetween(Duration start, Duration end)
DurationAssert isCloseTo(Duration expected, Duration offset)
// Duration properties
DurationAssert isZero()
DurationAssert isNotZero()
DurationAssert isPositive()
DurationAssert isNegative()
DurationAssert hasMillis(long millis)
DurationAssert hasNanos(long nanos)
DurationAssert hasSeconds(long seconds)
DurationAssert hasMinutes(long minutes)
DurationAssert hasHours(long hours)
DurationAssert hasDays(long days)Usage examples:
Duration short = Duration.ofMinutes(30);
Duration long = Duration.ofHours(2);
Duration zero = Duration.ZERO;
assertThat(short)
.isPositive()
.isLessThan(long)
.hasMinutes(30);
assertThat(zero).isZero();
assertThat(long)
.hasHours(2)
.isGreaterThan(short)
.isBetween(Duration.ofMinutes(90), Duration.ofHours(3));Assertions for Period objects representing date-based durations.
PeriodAssert assertThat(Period actual)
// Period comparison methods
PeriodAssert isEqualTo(Period expected)
PeriodAssert isNotEqualTo(Period expected)
// Period properties
PeriodAssert isZero()
PeriodAssert isNotZero()
PeriodAssert isPositive()
PeriodAssert isNegative()
PeriodAssert hasYears(int years)
PeriodAssert hasMonths(int months)
PeriodAssert hasDays(int days)Usage examples:
Period oneYear = Period.ofYears(1);
Period sixMonths = Period.ofMonths(6);
Period thirtyDays = Period.ofDays(30);
assertThat(oneYear)
.isPositive()
.hasYears(1)
.hasMonths(0)
.hasDays(0);
assertThat(Period.ZERO).isZero();Assertions for YearMonth objects.
YearMonthAssert assertThat(YearMonth actual)
// YearMonth comparison methods
YearMonthAssert isBefore(YearMonth yearMonth)
YearMonthAssert isAfter(YearMonth yearMonth)
YearMonthAssert isEqualTo(YearMonth expected)
// YearMonth properties
YearMonthAssert hasYear(int year)
YearMonthAssert hasMonth(Month month)
YearMonthAssert hasMonthValue(int month)
YearMonthAssert isLeapYear()
YearMonthAssert isNotLeapYear()Temporal offset utilities for precise time comparisons.
// Temporal offset factory methods
TemporalUnitWithinOffset within(long value, TemporalUnit unit)
TemporalUnitLessThanOffset byLessThan(long value, TemporalUnit unit)
TemporalUnitWithinOffset within(Duration duration)
// Common offset usage
within(5, ChronoUnit.MINUTES)
within(30, ChronoUnit.SECONDS)
byLessThan(1, ChronoUnit.HOURS)
within(Duration.ofMillis(100))Usage examples:
LocalDateTime now = LocalDateTime.now();
LocalDateTime almostNow = now.plusSeconds(2);
assertThat(almostNow)
.isCloseTo(now, within(5, ChronoUnit.SECONDS));
Instant start = Instant.now();
Instant end = start.plusMillis(50);
assertThat(end)
.isCloseTo(start, byLessThan(100, ChronoUnit.MILLIS));// Temporal offset classes
class TemporalUnitWithinOffset {
static TemporalUnitWithinOffset within(long value, TemporalUnit unit)
static TemporalUnitWithinOffset within(Duration duration)
}
class TemporalUnitLessThanOffset {
static TemporalUnitLessThanOffset byLessThan(long value, TemporalUnit unit)
}
// Java time enums and interfaces
enum ChronoUnit implements TemporalUnit {
NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS,
WEEKS, MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA, ERAS
}
enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}
enum DayOfWeek {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
// Zone classes
class ZoneId {
static ZoneId of(String zoneId)
static ZoneId systemDefault()
}
class ZoneOffset extends ZoneId {
static ZoneOffset of(String offsetId)
static ZoneOffset ofHours(int hours)
static ZoneOffset UTC
}Install with Tessl CLI
npx tessl i tessl/maven-org-assertj--assertj-core