【发布时间】:2012-09-30 04:35:56
【问题描述】:
我在我的项目中使用 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 用户?你会如何编写单元测试?
【问题讨论】:
标签: unit-testing testing grails spring-security