DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Private Remote Maven Repository With Artipie

Trending

  • Mastering Advanced Aggregations in Spark SQL
  • Infrastructure as Code (IaC) Beyond the Basics
  • The Future of Java and AI: Coding in 2025
  • Memory Leak Due to Time-Taking finalize() Method

Verifying Datetime and Date With Hamcrest

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

By 
Lubos Krnac user avatar
Lubos Krnac
·
Apr. 05, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
33.9K Views

Join the DZone community and get the full member experience.

Join For Free
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:

@Test
 public void validateDate() {
 //GIVEN
 Date expectedDate = new Date();

 //WHEN
 Date actualDate = new Date();

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

We can also do this for Java 8 types:

@Test
 public void validateDateTime() {
 //GIVEN
 LocalDateTime expectedDateTime = LocalDateTime.now();

 //WHEN
 LocalDateTime actualDateTime = LocalDateTime.now();

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

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

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

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

 //THEN
 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.

Hamcrest

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

Opinions expressed by DZone contributors are their own.

Related

  • Private Remote Maven Repository With Artipie

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: