Unit testing 如何在Grails单元测试中使用Spock模拟密码编码器

Unit testing 如何在Grails单元测试中使用Spock模拟密码编码器,unit-testing,grails,spring-security,mocking,spock,Unit Testing,Grails,Spring Security,Mocking,Spock,在如何模拟Grails单元测试中使用的自动连接依赖项方面,我可以提供一些建议。我省略了大部分不必要的代码,只给出了测试类和测试文件类中的相关方法 class UserService { def springSecurityService // spring bean def passwordEncoder // auto wired as per //https://stackoverflow.com/questions/33303585/spring-//secur

在如何模拟Grails单元测试中使用的自动连接依赖项方面,我可以提供一些建议。我省略了大部分不必要的代码,只给出了测试类和测试文件类中的相关方法

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
    }
我的测试

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
    }
我试图存根密码编码器,并让它返回真

def stubPasswordEncoder =  Stub(passwordEncoder) {
    isPasswordValid(_, _, _) >> true
 }
 service.passwordEncoder = stubPasswordEncoder
但这会给出一条错误消息:

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

有没有办法用Spock模拟这个依赖关系?

Stub和mock接受一个类-你给它一个空的实例-因此出现了异常

您应该能够这样模拟它:

def mockPasswordEncoder = Mock(PasswordEncoder) 
// note this is the class 
// org.springframework.security.crypto.password.PasswordEncoder

我试过Enrichegeson的方法,结果成功了! 我首先将PasswordEncoder导入测试类

import org.springframework.security.crypto.password.PasswordEncoder
然后执行正常的模拟程序。我最初感到困惑,因为他们测试的类只是通过定义隐式地创建了类的实例

def stubPasswordEncoder =  Stub(PasswordEncoder) {
        isPasswordValid(_, _, _) >> true
    }
    service.passwordEncoder = stubPasswordEncoder
我还找到了另一个不需要嘲笑的解决方案

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

这两种方法都很有效。谢谢你的帮助

此外,不要从测试中的
块或任何其他地方调用
setup()
。Spock负责调用
setupSpec()
setup()
cleanup()
cleanupSpec()
。它甚至可以确保,如果基本规范和子类规范都有各自的fixture方法,那么它们不会被覆盖,而是按照定义的顺序被调用。
service.passwordEncoder = [ isPasswordValid: { String rawPass, String salt, Null -> true } ]