我为我的项目使用了Grails Spring Security Plugin,现在想单元测试我的代码。我有下面的代码在我的控制器:Grails Spring Security Testing
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)
我如何可以验证弹簧安全用户在一个测试案例中?你会如何编写单元测试?
在我的控制,我有: 类UserController的{ \t \t高清springSecurityService ... – Nadya
我应该写下来的ControllerTest? – Nadya
@ user1726376不,你不应该在ControllerTest中需要它。我只是确保你有一个'springSecurityService'的引用,因为你有错误消息“无法获得属性'currentUser'在空对象上”。 –