Search code examples
unit-testinggrailsgrails-controller

Unit Testing Controllers in Grails 1.3.7: redirectUrl is null or params isn't available


I have the following code:

void testConfirmBookNotFound() {

    def book = new Book(id: 1, title: 'test book')
    mockDomain(Book, [book])
    mockDomain(BookProject, [new BookProject(id: 1, book: book)])

    def controller = new CancelController()
    controller.params.id = 5140
    controller.confirm()

    assertEquals "/admin/cancel/index", controller.response.redirectedUrl
}

If my test class extends ControllerUnitTestCase then controller.response.redirectedUrl is always null. However, if my test class extends GrailsUnitTestCase (which I've read is the fix for the null url issue) then I get No such property: params. What do I need to do to get this test to work?


Solution

  • Try using controller.redirectArgs.action as in...

    assertThat(controller.redirectArgs.action, equalTo("index"))