Testing Grails集成测试过滤器
有没有办法在集成测试中测试使用过滤器的控制器 似乎有一种方法可以在单元测试中使用@Mock注释,而不是在withFilter闭包上包装控制器调用 但是我不能在集成测试中测试过滤器,从我的观点来看,集成测试应该是非常直接的 更新 这就是我找到的解决方案。我没有使用@Mock注释,而是实例化了FiltersUnitTestMixin类并用必要的值填充它Testing Grails集成测试过滤器,testing,grails,filter,integration,Testing,Grails,Filter,Integration,有没有办法在集成测试中测试使用过滤器的控制器 似乎有一种方法可以在单元测试中使用@Mock注释,而不是在withFilter闭包上包装控制器调用 但是我不能在集成测试中测试过滤器,从我的观点来看,集成测试应该是非常直接的 更新 这就是我找到的解决方案。我没有使用@Mock注释,而是实例化了FiltersUnitTestMixin类并用必要的值填充它 public class ControllerTest { def controller = new Controller()
public class ControllerTest {
def controller = new Controller()
FiltersUnitTestMixin f = new FiltersUnitTestMixin()
@Before
public void setup() {
f.grailsApplication = grailsApplication
f.applicationContext = grailsApplication.mainContext
f.mockFilters(ControllerFilters)
}
@Test
public void shouldPassTheTest() {
f.withFilters(action:"actionName") {
controller.actionName()
}
}
}
我也有同样的问题,我发现这个-> 并根据我的需要进行了修改,以这个结尾
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)
}
}