Grails单元测试失败:此实现的GORM目前不支持像[executeQuery]这样的基于字符串的查询

时间:2018-05-24 19:36:51

标签: hibernate grails hql gorm spock

关于这一点还有其他问题,但它们已经很老了,没有一个能解决我的问题。

我从单元测试中收到以下错误:

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)

以下示例都是隐藏公司信息的通用示例。

破坏测试的一行是:

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

当前测试看起来像这样(只是当前跟踪数据):

    @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}"
        }
    }

我的设置:

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

谢谢,如果我需要提供更多信息,请告诉我们!

1 个答案:

答案 0 :(得分:0)

尝试使用HibernateTestMixin。它支持使用Hibernate 4进行基于字符串的HQL查询。

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}"
    }
}

您还需要添加到build.gradle

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