AssertJ assertions for Joda-Time provides assertions for Joda-Time types like DateTime and LocalDateTime.
If you need additional assertions, just create a ticket in the AssertJ Joda-Time issue tracker.
AssertJ Joda-Time is hosted on github : https://github.com/joel-costigliola/assertj-joda-time.
To quickly start using Joda-Time assertions, follow the steps below.
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-joda-time</artifactId>
<!-- use 1.1.0 for Java 7 projects -->
<version>2.2.0</version>
<scope>test</scope>
</dependency>
If you use another dependency tool, please check this page to find the relevant assertj-joda-time dependency declaration for your tool.
... and use your preferred IDEs code completion after assertThat.
Examples :
import static org.assertj.jodatime.api.Assertions.assertThat;
...
assertThat(dateTime).isBefore(firstDateTime);
assertThat(dateTime).isAfterOrEqualTo(secondDateTime);
// you can use Strings in comparisons to avoid a conversion (we do that for you)
assertThat(new DateTime("2000-01-01")).isEqualTo("2000-01-01");
// compare DateTimes ignoring seconds and milliseconds in the comparison
dateTime1 = new DateTime(2000, 1, 1, 23, 50, 0, 0, UTC);
dateTime2 = new DateTime(2000, 1, 1, 23, 50, 10, 456, UTC);
// assertion succeeds
assertThat(dateTime1).isEqualToIgnoringSeconds(dateTime2);
For DateTime assertions, comparison is performed in the DateTimeZone of DateTime to test, consequently the following assertion passes :
DateTime utcTime = new DateTime(2013, 6, 10, 0, 0, DateTimeZone.UTC);
DateTime cestTime = new DateTime(2013, 6, 10, 2, 0, DateTimeZone.forID("Europe/Berlin"));
assertThat(utcTime).as("in UTC time").isEqualTo(cestTime);
You can compare DateTime to another DateTime, or LocalDateTime to LocalDateTime, but not DateTime to LocalDateTime, it doesn't make sense as one is timezone dependent and the other is not.
Note that you can find more working examples in the JodaTimeAssertionsExamples.java class of the assertj-examples project.
To be even easier to use, one can specify DateTime or LocalDateTime with their String representation to avoid manual String conversion, like in the example below :
Example :
// instead of writing ...
assertThat(dateTime).isBefore(new DateTime("2004-12-13T21:39:45.618-08:00"));
// ... you can simply write (if you prefer)
assertThat(dateTime).isBefore("2004-12-13T21:39:45.618-08:00");
It is possible to compare DateTime or LocalDateTime ignoring time fields below a given a precision. This is handy if you don't want, for example, to consider minutes in the comparison (which also implies to ignore seconds and milliseconds too).
Example :
import static org.assertj.jodatime.api.Assertions.assertThat;
// compare DateTime or LocalDateTime with a precision level, ignoring time fields
dateTime1 = new DateTime(2000, 1, 1, 23, 50, 0, 0, UTC);
dateTime2 = new DateTime(2000, 1, 1, 23, 50, 10, 456, UTC);
// ignore seconds and milliseconds in the comparison
assertThat(dateTime1).isEqualToIgnoringSeconds(dateTime2);
Release date : 2018-06-15
Thanks to Eugene Strepetov for his contributions.
LocalDate localDate = new LocalDate(2000, 1, 1);
assertThat(localDate).hasYear(2000)
.hasMonthOfYear(1)
.hasDayOfMonth(1)
.isBefore(new LocalDate(2000, 1, 2))
.isBefore("2000-01-02")
.isBeforeOrEqualTo(new LocalDate(2000, 1, 1))
.isBeforeOrEqualTo("2000-01-01")
.isBeforeOrEqualTo(new LocalDate(2000, 1, 2))
.isBeforeOrEqualTo("2000-01-02")
.isEqualTo(new LocalDate(2000, 1, 1))
.isEqualTo("2000-01-01")
.isAfterOrEqualTo(new LocalDate(2000, 1, 1))
.isAfterOrEqualTo("2000-01-01")
.isAfterOrEqualTo(new LocalDate(1999, 12, 31))
.isAfterOrEqualTo("1999-12-31")
.isAfter(new LocalDate(1999, 12, 31))
.isAfter("1999-12-31")
.isNotEqualTo("2000-01-15")
.isNotEqualTo(new LocalDate(2000, 1, 15))
.isIn(new LocalDate(1999, 12, 31), new LocalDate(2000, 1, 1))
.isIn("1999-12-31", "2000-01-01")
.isNotIn(new LocalDate(1999, 12, 31), new LocalDate(2000, 1, 2))
.isNotIn("1999-12-31", "2000-01-02");
Release date : 2018-05-27
Thanks to John Killmer for his contributions.
DateTime dateTime = new DateTime(1999, 12, 31, 23, 59, 59, 999, DateTimeZone.UTC);
assertThat(dateTime).hasYear(1999)
.hasMonthOfYear(12)
.hasDayOfMonth(31)
.hasHourOfDay(23)
.hasMinuteOfHour(59)
.hasSecondOfMinute(59)
.hasMillisOfSecond(999);
Release date : 2016-04-10
AssertJ Joda Time 2.0.0 requires Java 8 as it relies on AssertJ Core 3.x, use 1.1.0 version if you are still using Java 7.
Thanks to Pascal Schumacher and David Simmons for their contributions.
Release date : 2013-07-14
2013-03-26 : 1.0.0 release
First release after Fest fork.
Latest javadoc release : AssertJ Joda-Time javadoc.
Migrating from Fest Joda-Time to AssertJ Joda-Time Assertions is easy, you only have to change your static import, just replace :
import static org.fest.assertions.api.JODA_TIME
by :
import static org.assertj.joda-time.api.Assertions
For linux users, a quick way to migrate is to use the following command:
find . -name "*.java" -exec sed -i "s/import static org.fest.assertions.api.JODA_TIME/import static org.assertj.jodatime.api.Assertions/g;s/import org.fest.assertions.api.JODA_TIME;/import org.assertj.jodatime.api.Assertions;/g;s/JODA_TIME./Assertions./g" '{}' \;
For mac users, you have an almost identical command (notice the "" after -i) :
find . -name "*.java" -exec sed -i "" "s/import static org.fest.assertions.api.JODA_TIME/import static org.assertj.jodatime.api.Assertions/g;s/import org.fest.assertions.api.JODA_TIME;/import org.assertj.jodatime.api.Assertions;/g;s/JODA_TIME./Assertions./g" '{}' \;
Note: if your project uses both FestAssert Core assertions and Fest Joda-Time assertions, execute the above scripts for Joda-Time before those for FestAssert Core!
You will have to make two static import : one for org.assertj.core.api.Assertions.assertThat to get core assertions and one org.assertj.jodatime.api.Assertions.assertThat for Joda-Time assertions.
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.jodatime.api.Assertions.assertThat;
...
// assertThat comes from org.assertj.jodatime.api.Assertions.assertThat static import
assertThat(new DateTime("2000-01-01")).isAfter(new DateTime("1999-12-31"));
// assertThat comes from org.assertj.core.api.Assertions.assertThat static import
assertThat("hello world").startsWith("hello");
If you have any questions, please use the AssertJ google group.
AssertJ Joda-Time is hosted on github : https://github.com/joel-costigliola/assertj-joda-time.
Please report bugs or missing features in the AssertJ Joda-Time issue tracker.
Thanks for your interest ! Please check our contributor's guidelines.