0

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)

miThom
  • 373
  • 2
  • 11
  • Could you provide the mapping between java 8 dates and database/hibernate dates? When you say java 8 dates do you mean you are using java.time.LocalDateTime? Could you also provide any error message you are getting? – Joe Oct 05 '16 at 22:46
  • ZonedDateTime in this case, you want the whole error stack or just the error name? – miThom Oct 06 '16 at 20:06
  • `grails.gorm.default.mapping = { "user-type"(type: org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTime, class: java.time.LocalDateTime) "user-type"(type: org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime, class: java.time.ZonedDateTime) }` – miThom Oct 06 '16 at 20:12
  • Yes edit your question adding the above mapping detail and the code of the test including how you set the foo.startDate variable. The more details the better we can understand. Add the details to your question marked as code instead of using the comments. – Joe Oct 07 '16 at 14:30

1 Answers1

0

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.

Rotem
  • 1,381
  • 1
  • 11
  • 23
  • i think it is the other way around. In this example (i'll update the question) I used ZonedDateTime, which the testing framework does not know (it knows java.util.date) – miThom Oct 06 '16 at 20:08