Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 无法为类LoginService创建模拟。模拟非接口类型需要CGLIB库。。?_Unit Testing_Grails_Spock - Fatal编程技术网

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

Unit testing 无法为类LoginService创建模拟。模拟非接口类型需要CGLIB库。。?,unit-testing,grails,spock,Unit Testing,Grails,Spock,我使用的是Grails2.4.2,默认情况下它已经安装了spock,对吗?嗯,我的一个控制器测试不太正常。我试图模拟我的几个服务,但我一直遇到以下错误: Failure: confirmEmailAddress() when verification failed(com.zee.RegistrationControllerSpec) | org.spockframework.mock.CannotCreateMockException: Cannot create mock for cl

我使用的是Grails2.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)
我在网上找不到关于这件事的任何信息。我的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....
    }
}
dependencies { 
        compile 'org.objenesis:objenesis:1.4'
        compile "cglib:cglib:2.2"
}
plugins {
             compile ':cache:1.1.7'
}
我的服务更简单:

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

我被踩了。即使是圣杯清洁也不起作用。。有什么帮助吗?D:

在您的
buildConfig.groovy
中, 替换此行:
':缓存:1.1.7'
':缓存:1.1.6'
像这样:

结果表明cglib依赖项已从缓存插件中删除

编辑: 如果仍要使用
cache:1.1.7
,只需在
buildConfig.groovy
中添加cglib依赖项,如下所示:

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....
    }
}
dependencies { 
        compile 'org.objenesis:objenesis:1.4'
        compile "cglib:cglib:2.2"
}
plugins {
             compile ':cache:1.1.7'
}