BenHuman
BenHuman

Reputation: 175

Can Hibernate criteria query in Grails be unit tested?

I am trying to write unit tests for a generic code base (uses Grails 2.5.1) without unit tests so that every part of code is tested. But I am having difficulties with testing criteria queries with Spock in all cases. Only eq can be tested - if 'in' or sqlRestriction is used it cannot be tested.

I am stuck, is there a better way to do this?

Upvotes: 2

Views: 596

Answers (1)

Sudhir N
Sudhir N

Reputation: 4096

HibernateTestMixin uses Hibernate 4 and a H2 in-memory database. This makes it possible to use all GORM features also in Grails unit tests.

All features of GORM for Hibernate can be tested within a HibernateTestMixin unit test including:

  • String-based HQL queries
  • composite identifiers
  • dirty checking methods
  • any direct interaction with Hibernate

The implementation behind HibernateTestMixin takes care of setting up the Hibernate with the in-memory H2 database. It only configures the given domain classes for use in a unit test. The @Domain annotation is used to tell which domain classes should be configured.

Add following dependency to buildconfig

dependencies {
        test 'org.grails:grails-datastore-test-support:1.0-grails-2.4'
    }

Add use the test mixins

@Domain(Person)
@TestMixin(HibernateTestMixin)

Source: grails 2.5.1 reference documentation

Upvotes: 2

Related Questions