SO上还有其他问题,但是它们已经很老了,没有一个可以解决我的问题。
我从单元测试中收到以下错误:
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 upgraded to 5 with no change in behavior)
Java 1.8
Groovy 2.4.7
最佳答案
尝试使用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'
}
关于hibernate - Grails单元测试失败:此实现GORM当前不支持基于字符串的查询,例如[executeQuery],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50516956/