I have written one filter rule which I want to test using grails integration tests. Filter is
invalidAccess(controller: "home") {
before = {
redirect(controller: "newHome", action: "index")
return false
}
}
I have followed this link to write the integration test http://ldaley.com/post/392153102/integration-testing-grails-filters
It returns result as false But gives
null for redirectedUrl
instead of newHome & index method url. What am I missing here?
import grails.util.GrailsWebUtil
class MyFilterTests extends GroovyTestCase {
def filterInterceptor
def grailsApplication
def grailsWebRequest
def request(Map params, controllerName, actionName) {
grailsWebRequest = GrailsWebUtil.bindMockWebRequest(grailsApplication.mainContext)
grailsWebRequest.params.putAll(params)
grailsWebRequest.controllerName = controllerName
grailsWebRequest.actionName = actionName
filterInterceptor.preHandle(grailsWebRequest.request, grailsWebRequest.response, null)
}
def getResponse() {
grailsWebRequest.currentResponse
}
def testFilterRedirects() {
def result = request( someParameter: "2", "home", "index")
assertFalse result
assertTrue response.redirectedUrl.endsWith(/* something */)
}
}
If you want to try
unit testingand need to mock some services then you can mock like:and you want
integration testthen try following testRef# Grails Integration Test Filter