我有named query的domain
:
class Publication {
String title
String author
static namedQueries = {
publicationsWithBookInTitle {
like 'title', '%Book%'
}
}
@Override
String toString() {
return title
}
}
在
controller
中,我有def show() {
Publication publicationInstance = Publication.publicationsWithBookInTitle.get()
println "Get: ${publicationInstance}"
println "List: ${Publication.publicationsWithBookInTitle.list()}"
respond publicationInstance
}
和我的
unit test
:void "test show"() {
given:
Publication publication = new Publication(title: 'MyBook', author: 'MB').save(flush: true, failOnError: true)
when:
controller.show()
then:
model.publicationInstance == publication
}
当我运行测试时,使用get()方法给我空值,并且测试失败。系统输出为
Get: null
List: [MyBook ]
我搜索此文件,但未找到此问题的任何解决方案。
为什么会发生这种情况?此问题的解决方案或解决方法是什么?
注意:-我当前正在使用
grails 2.3.9
。但是我的项目最初是使用grails 2.3.5
创建的,然后又升级为grails 2.3.9
。
最佳答案
最后,在dmahapatro注释的帮助下,我找出了根本原因(@dmahapatro 谢谢)。
命名查询在grails 2.3.9
中给出正确的结果。但是我的项目是从使用grails 2.3.5
的hibernate:3.6.10.6
升级的,并且没有按照grails 2.3.9 release notes的建议更改为hibernate:3.6.10.15
。升级项目休眠版本后,问题就解决了。
注:-如果使用grails 2.3.5
,则将面临此问题。
关于unit-testing - grails在单元测试中使用get()时,命名查询给出null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261723/