5

I use the Grails Spring Security Plugin for my project and now want to unit test my code. I have the following code in my controller:

def index() {
    redirect action: 'show', params: [id: springSecurityService.currentUser.id]
}

My Test Class has the following code:

void testIndex() {      
    controller.index()
    assert "/user/list" == response.redirectedUrl
}

This test fails:

| 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)

How can I authenticate a spring security user in a test case? How would you write the unit test?

3 Answers 3

7

You have to use functional tests for security. Unit tests use mocking but don't have plugins available, or a real request. Spring Security is implemented with a filter chain, so you need a real running server. If you use mocks, you're just testing the mocking.

4

For something this simple I wouldn't bother with complicated mocks, a straightforward

controller.springSecurityService = [currentUser:[id:1]]

would be sufficient.

2
  • This doesn't work. I get org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{currentUser={id=1}}' with class 'java.util.LinkedHashMap' to class 'grails.plugin.springsecurity.SpringSecurityService' due to: groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: currentUser for class: grails.plugin.springsecurity.SpringSecurityService
    – lmo
    Dec 3, 2014 at 23:30
  • @lmo the mock I've suggested will only work if you use def springSecurityService in your controller, it won't work if you have a typed declaration SpringSecurityService springSecurityService Dec 4, 2014 at 9:43
0

It appears that your reference to springSecurityService is null. As long as you have a field in your controller named springSecurityService, it should be injected. Are you using it as a local variable only in your index method and did not declare it as a field?

My UserController is as follows:

class UserController {

    /**
     * Dependency injection for the springSecurityService.
     */
    def springSecurityService

    ....
 }

UPDATE

Based on your comments to this answer, you did declare a springSecurityService field in your controller. I took my working application and tried a test that mirrors yours with my controller method:

@TestFor(UserController)
class UserControllerTests {

    void testSomething() {
        controller.register()
    }
}

I got a NullPointerException as well. From Burt's answer, (I did not know this), I think the springSecurityService instance is null in the contexts of the Unit Test execution.

3
  • In my controller I have: class UserController { def springSecurityService ...
    – dildik
    Oct 9, 2012 at 19:53
  • Should I write that down in the ControllerTest?
    – dildik
    Oct 9, 2012 at 19:54
  • @user1726376 No, you shouldn't need it in the ControllerTest. I was just making sure you had a reference to springSecurityService due to the fact that you had the error message "Cannot get property 'currentUser' on null object". Oct 9, 2012 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.