Friday, December 11, 2015

DateMatchers for unit testing a Date object

Hey, there is no DateMatcher in the HamCrest! Why?

I found a useful library: hamcrest-date. Really cool for unit testing:

assertThat(dola.getTranslationStatus().getResultDate(), is(isToday()));
assertThat(dola.getTranslationStatus().getResultDate(), is(sameDay(new Date())));


Wednesday, December 9, 2015

java Date and equals()

The system receive a Date object from the UI after the JSON conversion, than he try to find the related record in the database and compare the changes.

We use Jackson JSON with Sping annotation on REST API, and Sping Data to read the database.

We found the Date objects are instance of java.util.Date and other extensions. In this case the java.util.Date.equals() is not useful, he could not evaluate because of the different date representation.


If you would like to use the checking you have to use the compare() function. And also modify the generated equals() function of  your Class:

if (publishDate == null) {
if (other.publishDate != null) {
return false;
}
} else if (other.publishDate == null || 0 != publishDate.compareTo(other.publishDate)) {
return false;
}