[英]Grails - “Cannot get property 'grails' on null object” from unit test
[英]grails named queries giving null when using get() in unit test
我有一個名為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 THANKS )。
命名查詢在grails 2.3.9
給出正確的結果。 但是我的項目已從使用hibernate:3.6.10.6
grails 2.3.5
升級,並且沒有按照grails 2.3.9發行說明中的建議更改為hibernate:3.6.10.15
。 升級項目休眠版本后,問題就解決了。
注意:-如果使用grails 2.3.5
則會遇到問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.