[英]Unit Testing show action of a controller with findwhere in grails
[英]Grails unit testing: error while redirecting to an action of another controller
我有一個看起來像這樣的控制器動作
//An action inside itemUserController
def commentItem() {
String comment = params['itemComment']
User user = session['currentUser']
String title = params['title']
def isBook = false, isTVShow = false, isMovie = false
//I do some manips here......
//and finally...
if(isBook)
redirect(controller: "book", action: "show", id: book.id)
if(isMovie)
redirect(controller: "movie", action: "show", id: movie.id)
if(isTVShow)
redirect(controller: "TVShow", action: "show", id: tvShow.id)
}
我的問題是對此進行測試,並在單元測試和集成中進行了嘗試,但是在包含重定向的行中出現了相同的錯誤“ NullPointerException”(重定向中的書,電影和TVShow控制器看起來為null,我不知道如何注入它們,因為它們不是控制器類中的字段)。
java.lang.NullPointerException
at ivvq.ItemUserController.$tt__commentItem(ItemUserController.groovy:138)
這是我在測試類中的代碼部分:
when: "User tries to comment an item with an empty comment title and an empty comment text"
itemUserController.session['currentUser'] = user;
itemUserController.params['itemComment'] = ""
itemUserController.params['title'] = ""
itemUserController.params['itemBookId'] = book.id
itemUserController.commentItem()
then: "The user has an error message and the comment isnt posted"
itemUserController.flash.error != ""
很快,我打算做的是讓用戶對項目進行評論,並在提交評論后對同一項目進行評論。
注意:我正在使用Grails 2.3.11
我遇到了類似的情況,最終改變了我的重定向樣式。 現在我使用這樣的東西:
redirect(uri: "/controller/action", params: redirectParams)
對於您的情況,重定向如下所示:
redirect(uri: "/book/show", params: [id: book.id])
注意:我沒有對此進行測試,但是應該非常接近。 params
只是要發送給下一個方法的參數映射。 那時,這對我的所有單元測試都有效。
對重定向風格的文檔是在這里 ,如果是有幫助的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.