我有一个这样的控制器:
@Secured(['ROLE_USER','IS_AUTHENTICATED_FULLY'])
def userprofile(){
def user = User.get(springSecurityService.principal.id)
params.id = user.id
redirect (action : "show", params:params)
}
我想在spock中测试控制器上面的控制器,所以我写了这样的测试代码:
def 'userProfile test'() {
setup:
mockDomain(User,[new User(username:"amtoasd",password:"blahblah")])
when:
controller.userprofile()
then:
response.redirectUrl == "/user/show/1"
}
当我运行测试时,此测试失败并显示以下错误消息:
java.lang.NullPointerException: Cannot get property 'principal' on null object
at mnm.schedule.UserController.userprofile(UserController.groovy:33)
在集成测试的情况下:
class UserSpec extends IntegrationSpec {
def springSecurityService
def 'userProfile test'() {
setup:
def userInstance = new User(username:"antoaravinth",password:"secrets").save()
def userInstance2 = new User(username:"antoaravinthas",password:"secrets").save()
def usercontroller = new UserController()
usercontroller.springSecurityService = springSecurityService
when:
usercontroller.userprofile()
then:
response.redirectUrl == "/user/sho"
}
}
我也得到同样的错误。
什么地方出了错?
提前致谢。