AssertJ assertions for Joda-Time

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.0.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.

Using date string representation

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");
Comparing with a precision level

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 : 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.

  • BREAKING CHANGE : requires Java 8.
  • DateTime assertion : better handling of null in assertions. (David Simmons)
  • To be OS agnostic, replace \n with %n in error messages. (Pascal Schumacher)
  • Better spacing in Javadoc code examples. (Pascal Schumacher)

Release date : 2013-07-14

  • In DateTime assertions, comparison is performed in the DateTimeZone of the DateTime to check.
  • assertj-joda-time does not impose joda-time 2.2 version anymore, it is compatible with any 2.0+ version.
  • Multiline error message improvements to make it easier to see what was wrong.
  • Javadoc improvements : fix inconsistencies and add more code examples.

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.