gpt4 book ai didi

unit-testing - 无法为类 LoginService 创建模拟。非接口(interface)类型的模拟需要 CGLIB 库..?

转载 作者:行者123 更新时间:2023-12-02 13:46:24 24 4
gpt4 key购买 nike

我正在使用 grails 2.4.2,默认情况下它已经安装了 spock 对吗?好吧,我的一个 Controller 测试工作不太正常。我正在尝试模拟我的一些服务,但我不断收到此错误:

 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)

我真的无法在互联网上找到任何关于此的内容。我的 Controller 规范看起来像这样:
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....
}
}

我的服务更简单:
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)
}
}

我被跺了跺脚。即使是 grails clean 也无法解决问题..有什么帮助吗?丁:

最佳答案

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

plugins {
compile ':cache:1.1.6'
}

原来 cglib 依赖项已从缓存插件中删除。 source .

编辑:
如果你还想用 cache:1.1.7您可以在 buildConfig.groovy 中添加 cglib 依赖项像这样:
dependencies { 
compile 'org.objenesis:objenesis:1.4'
        compile "cglib:cglib:2.2"
}
plugins {
compile ':cache:1.1.7'
}

关于unit-testing - 无法为类 LoginService 创建模拟。非接口(interface)类型的模拟需要 CGLIB 库..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com