lundi 21 août 2017

Grails: Testing GORM criteria that require properties mapped by user-type (org.joda.time.DateTime)

I'm trying to test a method that uses a Criteria query to count matching object. This uses my Reservation domain class that has a property of type org.joda.time.DateTime

class Reservation {
  Organization organization
  DateTime dateTime
  //...

  int countPending(Organization org) {
    Reservation.createCriteria().count() {
      eq("organization", org)
      gte("dateTime", new DateTime())
      // ...
    } 
  }
}

Here is the specification I use to test that method:

@Mock([Reservation])
class ReservationSpec extends Specification {
  def "countPending should return the number of pending reservations"() {
    // ...
    expect:
    Reservation.countPending(org) == 0;
  }
}

When I try to run the test, I get:

java.lang.IllegalArgumentException: Property [dateTime] is not a valid property of class [Reservation]

I suspect this is because DateTime is neither a primitive type or a domain object, and the code that mocks domain objects doesn't know how to handle it. In order to run this code in the application we use a custom user-type mapping (org.joda.time.contrib.hibernate.PersistentDateTime).

Is there a way to make DateTime known to the domain mocking code, or any other way to make this testable?

Aucun commentaire:

Enregistrer un commentaire