How to mock springSecurityService in unit tests us

2019-07-18 22:27发布

问题:

I am using Spring security in my grails project. I have installed the spring-security-core plugin and spring-security-ui plugin.

The domain classes I have used for Person and Authority are User and Role respectively

As per project requirements, I have modified my User.groovy class and the code for the same is as below:

class User {
transient springSecurityService

//Mandatory Fields
String employeeId
String firstName
String lastName
String password
String emailId

//Other Fields
String mobileNumber
String address
String city
String zipCode

User manager

static hasMany = [previousPasswords: String]

boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired

static transients = ['springSecurityService']

static constraints = {
    employeeId blank: false, unique: true
    firstName blank: false
    lastName blank: false
    password blank: false, password: true, validator: { val, obj ->
        if(obj.previousPasswords) { 
            return !obj.previousPasswords.contains(encode(val.toUpperCase()))
        }
        return true
    }
    emailId blank: false, email: true

    mobileNumber nullable: true
    address nullable: true
    city nullable: true
    zipCode nullable: true

    manager nullable: true

    previousPasswords display: false, editable: false
}

static mapping = {
    password column: '`password`'
}

Set<Role> getAuthorities() {
    UserRole.findAllByUser(this).collect { it.role } as Set
}

def beforeInsert() {
    previousPasswords = [encode(password.toUpperCase())]
    encodePassword()
}

def beforeUpdate() {
    if (isDirty('password')) {
        previousPasswords << encode(password.toUpperCase())
        encodePassword()
    }
}

protected String encode(String pwd) {
    return springSecurityService.encodePassword(pwd)
}

protected void encodePassword() {
    password = springSecurityService.encodePassword(password)
}
}

I am trying to write Unit tests to check the constraints for this unit class

One of my test looks as below

    void "test if employeeId can be blank or non-unique"() {
    given:

    def springSecurityService = mockFor(SpringSecurityService,true)
    springSecurityService.encodePassword(){String pwd -> return null}

//      def springSecurityServiceFactory = mockFor(SpringSecurityService,true)
//      def mockSpringSecurityService = Mock(SpringSecurityService)
//      mockSpringSecurityService.metaClass.encodePassword = {String password ->     return null}
//      User.metaClass.encodePassword = { return null }
//      User.metaClass.encode = {password -> return null }
//      User.metaClass.getSpringSecurityService = { mockSpringSecurityService }

    when: 'employeeId is blank'
    def user = new User(employeeId: "   ")
    user.springSecurityService= springSecurityService

    then: 'validation fails'
    !user.validate()
    user.errors.getFieldError("employeeId").codes.contains("nullable")

    when: 'employeeId is unique'
    user = new User(employeeId: "empId1", firstName: "f_name", lastName: "l_name", password: "password", emailId: "test@hptest.com")
    user.springSecurityService= springSecurityService

    then: 'validation succeeds'
    user.validate()

    user.save(flush: true, failOnError: true)
    mockForConstraintsTests(User, [user])

    when: 'employeeId is non unique'
    def user_2 = new User(employeeId: "empId1")
    user_2.springSecurityService= springSecurityService     

    then: 'validation fails'
    !user_2.validate()
    user_2.errors.getFieldError("employeeId").codes.contains("unique")
}

I have been trying different ways to mock springSecurityService but all seem to fail. Can anyone suggest a way to mock this service.

Currently I am getting this error.

groovy.lang.MissingMethodException: No signature of method:                   grails.test.GrailsMock.encodePassword() is applicable for argument types: (com.hp.bots.UserSpec$_$spock_feature_0_0_closure1) values: [com.hp.bots.UserSpec$_$spock_feature_0_0_closure1@18324cd]
at com.hp.bots.UserSpec.test if employeeId can be blank or non-unique(UserSpec.groovy:25)

This test also fails if I try to run it as an integration test (without mocking). I am not able to understand where I am going wrong.

回答1:

I guess if you replace

springSecurityService.encodePassword(){String pwd -> return null}

with

  springSecurityService.demand.encodePassword(){String pwd -> return null}

and

    user.springSecurityService= springSecurityService.createMock() (grails 2.3.7) 
and for user_2

you should be able to get this test running.

you could have a look here for further documentation. http://grails.org/doc/2.3.7/guide/testing.html



回答2:

I usually mock services by creating a map with the function(s) that will be called. I would only recommend this approach when only one or two simple functions are being called on the service in question, and these calls do not need to be verified/tracked. In your case your given clause might look like this:

def springSecurityService = [encodePassword: { password ->
    return password
}]