9

I have a user class like such:

class User {
    transient springSecurityService
    String displayName
    String password
<snip>
    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}

And a UserController. I'm trying to write unit tests for the UserController however I'm getting this error for save, update and delete tests:

java.lang.NullPointerException: Cannot invoke method encodePassword() on null object

What do I have to set up in the way of mocking to get this to work?

I have tried many combinations of mocking code such as the following, but I am at a loss.

defineBeans {
    springSecurityService(SpringSecurityService)
}

Any advice would be greatly appreciated.

1
  • I'm experiencing the exact same issue. As far as I can see, defineBeans() should work, according to the documentation. Unfortunately, it has no effect. Apr 26, 2012 at 3:00

4 Answers 4

8

I personally don't like adding logic to production code to help satisfy a test. Sometimes you have to make a decision on what's best to do. Couple of options...

  1. The above answer will work, but as I stated I personally wouldn't prefer
  2. Don't unit test. Write all of your tests that run into this situation as integration tests.
  3. Mock it out with a fake service.

If this code (or code that runs into the same problem) is sprinkled throughout your application then you'd probably want to figure out a way to mock out these calls in your unit tests for all test cases so you're not duplicating your setup efforts everywhere. An easy way to mock this out is with metaClassing.

@Test
public void something() {
   def user = ...
   def springSecurityService = new Object()
   springSecurityService.metaClass.encodePassword = {String password -> "ENCODED_PASSWORD"}
   user.springSecurityService = springSecurityService
   ...
}

Now when the springSecurityService.encodePassword gets invoked it should return "ENCODED_PASSWORD". I also create an Object instead of instantiating a new SpringSecurityService because if you instantiate an actual service then you may end up calling actual methods on that service unexpectedly and unknowingly and having your tests pass for the wrong reasons. I'd rather get a no such method error than a passing test that shouldn't be passing.

1
  • Good answer! Btw I corrected the line "service.metaClass.encodePassword = " to "springSecurityService.metaClass.encodePassword = "
    – david
    Jan 11, 2012 at 15:59
0

I've done that like this:

    protected void encodePassword() {
        // SpringSecutiryService is not injected in tests.
        if (springSecurityService)
            password = springSecurityService.encodePassword(formPassword)
    }
1
  • 2
    That's not an ideal solution, looks more like a fix to get the tests passed. What if the security service is nulled for some odd reason in production... Then all your passwords don't get hashed (without an error message)
    – david
    Jan 11, 2012 at 16:02
0

I think the right thing to do is to mock out the service. You'll want to test the different cases that can be the return value and that the correct value is passed to the service method.

@Test
public void something() {
   def user = ...
   def expectedPassword = 'mock encoded pass' 
   controller.springSecurityService = [encodePassword: { String passwd -> return expectedPassword }]

   ...
}

or

@Test
public void something() {
   def user = ...
   def expectedPassword = 'mock encoded pass' 
   def mockSecurityService = mockFor(SpringSecurityService)
   mockSecurityService.demand.encodePassword { String passwd -> return expectedPassword}
   controller.springSecurityService = mockSecurityService.createMock()

   ...
   mockSecurityService.verify() // throws exception if demands aren't met
}
-1

In my case I tried to override SecUser's encodePassword() implementation -which calls springSecurityService.encodePassword().

I was surprised because I needed to override the class and the instance (if I didn't override any, it fails):

SecUser.metaClass.encodePassword = { 'a' }
user.metaClass.encodePassword = { 'b' }

any idea of why do I need this?

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.