Grails Integration Test Filter

与世无争的帅哥 提交于 2019-12-04 07:25:49

I had the same issue, I found this -> http://ldaley.com/post/392153102/integration-testing-grails-filters

And modified it to suit my needs ending with this

import grails.util.GrailsWebUtil
import org.junit.After
import org.junit.Before
import org.junit.Test

class SomethingIntegrationTests {

    def filterInterceptor
    def grailsApplication
    def grailsWebRequest

    @Before
    void setUp() {

    }

    @After
    void tearDown() {

    }

    @Test
    void testFilterRedirects() {

        def result = request("home", "index", someParameter: "2")
        assert !result
        assert response.redirectedUrl.endsWith(/* something */)
    }

    def getResponse() {
        grailsWebRequest.currentResponse
    }

    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)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!