Friday, November 2, 2012

FEST Assertions for Joda Time

Do you write unit tests? Of course you do. Do you use Joda Time? I think so. Do you use FEST Assertions? You should try it if you haven't yet. With FEST Assertions we can write fluent code like this:
assertThat(result).isEqualTo(expected);
assertThat(testRunSeconds).isLessThan(maxTestRunSeconds);
assertThat(someList).isNotNull().hasSize(3).contains("expectedEntry");
Now let's assume we have some functions that return joda DateTime, and we want to test it. Can we do this in FEST Assertions?
assertThat(resultDateTime).isAfter(timeframeBeginning).isBefore(timeframeEnd);
No, we can't :( FEST Assertions don't handle Joda Time classes. However, do not worry :) At SoftwareMill we have written our own TimeAssertions for that :) So you can write your code like this:
TimeAssertions.assertTime(someTime).isAfterOrAt(someOtherTime);
TimeAssertions works for org.joda.time.DateTime, java.util.Date and org.joda.time.LocalDateTime. You can freely exchange DateTime and Date, i.e. you can compare DateTime to Date, DateTime to DateTime etc. LocalDateTime can be compared only to instances of the same class, as it doesn't make sense to compare it to DateTime or Date without specifying the time zone. TimeAssertions is available on github. If you want to use it from Maven project, add repository:
<repository>
    <id>softwaremill-releases</id>
    <name>SoftwareMill Releases</name>
    <url>http://tools.softwaremill.pl/nexus/content/repositories/releases</url>
</repository>
And dependency:
<dependency>
    <groupId>pl.softwaremill.common</groupId>
    <artifactId>softwaremill-test-util</artifactId>
    <version>70</version>
</dependency>
Happy testing!

5 comments:

Tomasz Nurkiewicz said...

Worth to mention that Fest 2.0 supports assertions on standard Java Date class.

Can you consider moving your great contribution to root Fest project so it's available out-of-the-box?

Unknown said...

Cmon, who uses java.util.Date this days :P

Contributing to Fest 2.0 is a good idea, I'll try. Thanks!

Paweł Włodarski said...

Spoko, to będzie przydatne. Macie jeszcze jakieś ciekawe rzeczy w tych utilsach?

pzdr

Unknown said...

@Paweł Nie wiem co jest dla Ciebie ciekawe. Kod jest ogólnodostępny, więc zapraszam do sprawdzenia.

Paweł Włodarski said...

ej nie macie takiej supermarketingowej prezentacji - "softwaremill-test-util - I NIC NIE BĘDZIE NIGDY TAKIE SAMO!" ? :D