无法为类LoginService创建模拟.模拟非接口类型需要CGLIB库..?

shy*_*ldo 4 grails unit-testing spock

我正在使用grails 2.4.2,默认情况下已经安装了spock吗?好吧,我的一个控制器测试工作不正常.我试图模拟我的几个服务,但我一直收到这个错误:

 Failure:  confirmEmailAddress() when verification failed(com.zee.RegistrationControllerSpec)
|  org.spockframework.mock.CannotCreateMockException: Cannot create mock for class com.zee.LoginService. Mocking of non-interface types requires the CGLIB library. Please put cglib-nodep-2.2 or higher on the class path.
    at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:52)
    at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:51)
    at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
    at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:47)
    at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:282)
    at org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:83)
    at com.zee.RegistrationControllerSpec.setup(RegistrationControllerSpec.groovy:22)
Run Code Online (Sandbox Code Playgroud)

我无法在互联网上找到任何关于此的内容.我的controllerSpec看起来像这样:

import grails.test.mixin.TestFor
import spock.lang.Specification

@TestFor(RegistrationController)
class RegistrationControllerSpec extends Specification {

    LoginService loginService

    EmailAddressConfirmationService emailAddressConfirmationService

    EmailNotificationService emailNotificationService

    AccountRecordService accountRecordService

    def setup() {
        loginService = Mock()
        emailAddressConfirmationService = Mock()
        emailNotificationService = Mock()
        accountRecordService = Mock()

        controller.loginService = loginService
        controller.emailAddressConfirmationService = emailAddressConfirmationService
        controller.emailNotificationService = emailNotificationService
        controller.accountRecordService = accountRecordService
    }

    def cleanup() {
    }

    void "confirmEmailAddress() when verification failed"() {
        // some test here....
    }
}
Run Code Online (Sandbox Code Playgroud)

我的服务更简单:

import grails.transaction.Transactional

@Transactional
class LoginService {

    def registerUser(Login login) {
        login.pincode = "";
        login.groupId = Login.REGISTERED_USER_GROUP_ID
        login.save(flush :true)
    }

    public void userJoined(Login login) {
        login.save(flush: true)
    }
}
Run Code Online (Sandbox Code Playgroud)

我被踩了.即使是一个grails clean也不会有诀窍..任何帮助?d:

Pet*_*tra 17

在你的buildConfig.groovy,替换该行: ':cache:1.1.7'':cache:1.1.6' 像这样:

plugins {
         compile ':cache:1.1.6'
}
Run Code Online (Sandbox Code Playgroud)

事实证明,cglib依赖项已从缓存插件中删除.来源.

编辑:如果您仍想使用cache:1.1.7,可以在此处添加cglib依赖项buildConfig.groovy:

dependencies { 
        compile 'org.objenesis:objenesis:1.4'
????????compile "cglib:cglib:2.2"
}
plugins {
             compile ':cache:1.1.7'
}
Run Code Online (Sandbox Code Playgroud)