如何使用grails在单元测试中模拟springSecurityService

时间:2014-05-06 13:25:45

标签: unit-testing grails spring-security mocking integration-testing

我在grails项目中使用Spring安全性。我已经安装了spring-security-core插件和spring-security-ui插件。

我用于人员和权限的域类分别是用户和角色

根据项目要求,我修改了我的User.groovy类,其代码如下:

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

我正在尝试编写单元测试以检查此单元类的约束

我的一个测试看起来如下

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

我一直在尝试不同的方法来模拟springSecurityService,但似乎都失败了。任何人都可以建议一种方法来模拟这项服务。

目前我收到此错误。

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)

如果我尝试将其作为集成测试运行(无需模拟),此测试也会失败。我无法理解我哪里出错了。

2 个答案:

答案 0 :(得分:3)

我想如果你替换

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

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

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

你应该可以让这个测试运行。

您可以在这里查看更多文档。 http://grails.org/doc/2.3.7/guide/testing.html

答案 1 :(得分:1)

我通常通过创建一个包含将被调用的函数的映射来模拟服务。我只会在有问题的服务上调用一个或两个简单函数时推荐这种方法,并且不需要验证/跟踪这些调用。在您的情况下,您的给定子句可能如下所示:

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