我为我的项目使用了 Grails Spring Security 插件,现在想要对我的代码进行单元测试。我的控制器中有以下代码:
def index() {
redirect action: 'show', params: [id: springSecurityService.currentUser.id]
}
我的测试类有以下代码:
void testIndex() {
controller.index()
assert "/user/list" == response.redirectedUrl
}
此测试失败:
| Running 8 unit tests... 1 of 8
| Failure: testIndex(xxx.UserControllerTests)
| java.lang.NullPointerException: Cannot get property 'currentUser' on null object
at xxx.UserController.index(UserController.groovy:12)
at xxx.UserControllerTests.testIndex(UserControllerTests.groovy:19)
如何在测试用例中验证 Spring Security 用户?您将如何编写单元测试?