unit-testingdategrailsjava-8grails-2.5

java8 dates in grails 2.5.5 unit testing


I am using grails 2.5.5 with java 8. To get this working with the new dates, I defined a mapping between the java 8 dates (ZonedDateTime) and the database/hibernate dates. This works fine, no problem at all

here's where the problem starts: Unit Testing: I have a method which uses

Foo.withCriteria{
    ge("startDate",foo.startDate)
}

the problem now is that startDate is a ZonedDateTime and I get the error that startDate is no existent property of Foo. Using FindAllBy gives the same problem.

I cannot mock this method, for it is a private method. How do i get these java 8 dates working in unit tests?

(if I gave too little information, just ask, I can provide, but I thought this would be enough and I wanted to keep it as general as possible for stackOverflow)


Solution

  • Each date has a different format so you must check that the Date are of the same type and format. Your Java 8 probably using java.util.Date and SimpleDateFormatter.

    Please check your unit testing framework date format, it might be DateTime format so it causes the differences and may cause you error.

    On the following code:

    Foo.withCriteria{
        ge("startDate",foo.startDate)
    }
    

    Make sure that the foo.startDate was initialized correctly using your unit testing framework with the correct format.

    You can also use other date formats such as:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date convertedCurrentDate = sdf.parse(currentDateText);
    

    or ISO 8601 DateTime Format

    These are very popular formats using databases, which can explain why

    Using FindAllBy gives the same problem.