ry1633
ry1633

Reputation: 463

Grails integration test - redirect action returns null

This is a small integration Junit that I'm having difficulty with. I've re-written this several different ways and the current way is straight out of the Grails manual - but it still returns null. I don't see the error; I thought it might be a spelling error but I've checked all those. I've tried redirectUrl and redirectedUrl - still returns null.

Controller snippet:

@Transactional(readOnly = true)     
def saveReportError() {
    redirect(action:'reportError')  
}

Test:

@Test
void "test save error report"() {
controller.saveReportError()
    assertEquals '/reportSiteErrors/reportError', controller.response.redirectUrl
}

Upvotes: 0

Views: 744

Answers (1)

saw303
saw303

Reputation: 9072

I recommend to implement the test as a unit test like this.

import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(SimpleController)
class SimpleControllerSpec extends Specification {

    void 'test index'() {
        when:
        controller.index()

        then:
        response.redirectedUrl == '/simple/hello'
    }
}

Using a unit test has the advantage of speed.

Upvotes: 1

Related Questions