0

I could use some advice in how to mock an auto wired dependency used in a Grails unit test. I've omitted most of the unnecessary code and just given the test class and the relevant methods in the file class under test

class UserService {

    def springSecurityService // spring bean
    def passwordEncoder // auto wired as per 
    //https://stackoverflow.com/questions/33303585/spring-//security-encode-password-with-       bcrypt-algorithm

.....

    def passwordPreviouslyUsed(String newPassword, def userId){
        def passwordExists = false
        def usersPasswords = findPasswordsForUser(userId)
        usersPasswords.each{ password ->
            if (passwordEncoder.isPasswordValid(oldPassword, newPassword, null)) {
                passwordExists = true
            }
        }
        return passwordExists
    }

    .....
    def findPasswordsForUser(def userId){
        User foundUser = User.findById(userId)
        def passwordsForUser = UserPasswords.createCriteria().list {
            eq('user', foundUser) 
            projections{
                property('password')
            }
        }
        passwordsForUser
    }

My test

class UserServiceSpec extends Specification implements DataTest, ServiceUnitTest<UserService> {

    def passwordEncoder
    def setupSpec() {
        mockDomains User, UserPasswords
    }

    def setup() {
        def stubPasswordEncoder =  Stub(passwordEncoder) {
            isPasswordValid(_, _, _) >> true
         }
         service.passwordEncoder = stubPasswordEncoder
    }


    void "test for user passwordPreviouslyUsed"() {
        given: "a user already exists"
        setup()
        service.createNewUser("testName", "testy@test.com", "Secret1234" )
        //^(does some validation, then User.save())
        User foundUser = User.findByEmail("testy@test.com")
        foundUser.fullName == "testName"
        long testUserId = foundUser.id

        and: "we update the password for that user, and it to the userPasswords"
        UserPasswords newUserPassword = new UserPasswords(
            user: foundUser,
            password: "newPassword1"
        )
        newUserPassword.save()

        //use passwordPreviouslyUsed method to check a string with the same value as the 
        //previously 
        //updated password to check if it has already been used
        when: "we check if the password has been used before"

        def response = service.passwordPreviouslyUsed("newPassword1", fundsyId)

        then:
        response == true
    }

Without stubbing or mocking this dependency, I get the error

Cannot invoke method isPasswordValid() on null object

I tried to stub password encoder and have it return true

def stubPasswordEncoder =  Stub(passwordEncoder) {
    isPasswordValid(_, _, _) >> true
 }
 service.passwordEncoder = stubPasswordEncoder

But this gives an error message:

Stub in 'spock.mock.MockingApi' cannot be applied to         '(java.lang.Object, groovy.lang.Closure)'

Is there any way to mock this dependency with Spock?

Icarian
  • 117
  • 6

2 Answers2

1

Stub and Mock take a class - you're giving it an instance that is null - hence the exception.

You should be able to mock it as so:

def mockPasswordEncoder = Mock(PasswordEncoder) 
// note this is the class 
// org.springframework.security.crypto.password.PasswordEncoder
erichelgeson
  • 2,310
  • 1
  • 16
  • 24
  • 2
    Furthermore, don't call `setup()` from your `given:` block or anywhere else in your test. Spock takes care of calling `setupSpec()`, `setup()`, `cleanup()` and `cleanupSpec()`. It even makes sure that if base specification and subclass specification each have their fixture methods, they are not being overwritten but called in a defined order. – kriegaex Apr 10 '20 at 01:23
0

I tried enrichelgeson's approach, and it worked! I first imported PasswordEncoder to the test class

import org.springframework.security.crypto.password.PasswordEncoder

then implemented the normal mocking procedure. I was initially confused because they class under test just implicitly created an instance of the class by defining it.

def stubPasswordEncoder =  Stub(PasswordEncoder) {
        isPasswordValid(_, _, _) >> true
    }
    service.passwordEncoder = stubPasswordEncoder

I also found another solution that didn't require mocking

service.passwordEncoder = [ isPasswordValid: { String rawPass, String salt, Null -> true } ]

Both approaches work fine. Thanks for the help!

Icarian
  • 117
  • 6