Używam wtyczki Grails Spring Security do mojego projektu, a teraz chcę przetestować mój kod. Mam następujący kod w moim kontrolera:Grails Spring Security Testing
def index() {
redirect action: 'show', params: [id: springSecurityService.currentUser.id]
}
moja klasa testowa ma następujący kod:
void testIndex() {
controller.index()
assert "/user/list" == response.redirectedUrl
}
Ten test nie powiedzie się:
| 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)
Jak mogę uwierzytelnić użytkownika Zabezpieczenia wiosna w przypadku testowym? Jak byś napisał test jednostkowy?
W moim kontroler mam: klasy UserController { \t \t def springSecurityService ... – Nadya
powinienem napisać, że w dół w ControllerTest? – Nadya
@ user1726376 Nie, nie powinieneś go potrzebować w usłudze ControllerTest. Właśnie upewniałem się, że masz odniesienie do 'springSecurityService' ze względu na fakt, że masz komunikat o błędzie" Nie można pobrać własności 'currentUser' na obiekcie zerowym ". –