У меня есть такой контроллер:
@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"
}
}
я получаю ту же ошибку, что и Что ж.
Что пошло не так?
Заранее спасибо.