beechovsky
beechovsky

Reputation: 151

Grails Unit Test fails: String-based queries like [executeQuery] are currently not supported in this implementation of GORM

There are other questions on SO about this, but they are pretty old and none offer solutions to my issue.

I'm getting the following error from a unit test:

java.lang.UnsupportedOperationException: String-based queries like [executeQuery] are currently not supported in this implementation of GORM. Use criteria instead.
at org.grails.datastore.gorm.GormStaticApi.unsupported(GormStaticApi.groovy:984)
at org.grails.datastore.gorm.GormStaticApi.executeQuery(GormStaticApi.groovy:896)
at org.grails.datastore.gorm.GormStaticApi.executeQuery(GormStaticApi.groovy:892)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.executeQuery(GormEntity.groovy:1026)
at com.mycompany.SomeService.$tt__getSomeThing(SomeService.groovy:54)
at com.mycompany.SomeService.getSomeThing_closure1(SomeService.groovy)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
at com.mycompany.SomeServiceSpec.nominal getSomeThing(SomeServiceSpec.groovy:45)

The line that is breaking the test is:

def something = SomeDomain.executeQuery("some HQL string")

The test looks like this currently (just tracing data currently):

    @TestFor(SomeService)
    @Mock(SomeDomain)
    class SomeServiceSpec extends Specification {

        def setup() {
        }

        def cleanup() {
        }

        void "nominal getSomeThing"() {
            setup:
            def args = ['thingId':1l]

            when:
            def something = service.getSomething(args)

            then:
            println "Thing: ${something}"
        }
    }

My Setup:

Grails 3.1.12
Hibernate 4 (I upgraded to 5 with no change in behavior)
Java 1.8
Groovy 2.4.7

Upvotes: 3

Views: 1694

Answers (1)

doelleri
doelleri

Reputation: 19702

Try using the HibernateTestMixin. It supports string-based HQL queries with Hibernate 4.

import grails.test.mixin.TestMixin
import grails.test.mixin.gorm.Domain
import grails.test.mixin.hibernate.HibernateTestMixin

@TestFor(SomeService)
@Domain(SomeDomain)
@TestMixin(HibernateTestMixin)
class SomeServiceSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "nominal getSomeThing"() {
        setup:
        def args = ['thingId':1l]

        when:
        def something = service.getSomething(args)

        then:
        println "THING: ${something}"
    }
}

You will also need to add to your build.gradle:

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

Upvotes: 1

Related Questions