Over a million developers have joined DZone.

Verifying Datetime and Date With Hamcrest

The hamcrest-date library is a great tool for verifying dates in JUnit tests with Hamcrest.

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Since I started diving into automated testing and practicing TDD, verification of date values has been a  pain. Luckily there is a nice library for legacy Date and new Java 8 DateTime APIs, which cures this pain.

If you belong to healthier part of the Java development community and you're practicing unit testing on daily basis, you are probably are aware of the Hamcrest Java library. It can make your tests much more readable. Its architecture is very modular and is used by various other testing libraries.

A major part of its flexibility is it concept of a Matcher. I am not going to dive into this concept now. If you are not familiar, just take a quick look at the Hamcrest tutorial. One of the matchers you can plug into your testing toolbox is the library hamcrest-date. With this library we can easily test that a date was generated within certain range:

 public void validateDate() {
 Date expectedDate = new Date();

 Date actualDate = new Date();

 assertThat(actualDate, DateMatchers.within(2, ChronoUnit.SECONDS, expectedDate));

We can also do this for Java 8 types:

 public void validateDateTime() {
 LocalDateTime expectedDateTime = LocalDateTime.now();

 LocalDateTime actualDateTime = LocalDateTime.now();

 assertThat(actualDateTime, LocalDateTimeMatchers.within(2, ChronoUnit.SECONDS, expectedDateTime));

Or pick from various exotic verifications the hamcrest-core library provides:

 public void validateZonedDateTime() {
 ZonedDateTime expectedDateTime = ZonedDateTime.of(2016, 3, 20, 13, 3, 0, 0, ZoneId.of("GMT+1"));

 ZonedDateTime actualDateTime = ZonedDateTime.of(2016, 3, 20, 13, 3, 0, 0, ZoneId.of("GMT-0"));

 assertThat(actualDateTime, ZonedDateTimeMatchers.sameDay(expectedDateTime));
 assertThat(actualDateTime, ZonedDateTimeMatchers.after(expectedDateTime));
 assertThat(actualDateTime, ZonedDateTimeMatchers.isSunday());
 assertThat(actualDateTime, ZonedDateTimeMatchers.isMarch());

Kudos to the creator of this nice little library. This example is hosted in Github.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


Published at DZone with permission of Lubos Krnac, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}