lundi 2 novembre 2015

Grails unit testing: redirect calling an action of another controller

I have a controller action that looks like this

//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)
}

My problem is testing this, tried in unit tests as well as integration but am having the same error "NullPointerException" in the lines containing redirect (looks like the book, movie and TVShow controllers inside redirect are null and I have no idea on how to inject them as they aren't fields in the controller class).

java.lang.NullPointerException
at ivvq.ItemUserController.$tt__commentItem(ItemUserController.groovy:138)

Here is my part of code in the test class:

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 != ""

Shortly, what I am intending to do is letting a user comment on an item and render the same item commented after comment submission.

NB: I'm on Grails 2.3.11

Aucun commentaire:

Enregistrer un commentaire