Cannot create mock for class LoginService. Mocking of non-interface types requires the CGLIB library..?
10,481
In your buildConfig.groovy
,
Replace this line:
':cache:1.1.7'
with ':cache:1.1.6'
Like so:
plugins {
compile ':cache:1.1.6'
}
Turns out that the cglib dependency was removed from the cache plugin. source.
edit:
If you still want to use cache:1.1.7
you can just add the cglib dependency in your buildConfig.groovy
like this:
dependencies {
compile 'org.objenesis:objenesis:1.4'
compile "cglib:cglib:2.2"
}
plugins {
compile ':cache:1.1.7'
}
Author by
Admin
Updated on June 07, 2022Comments
-
Admin almost 2 years
I'm using grails 2.4.2 and by default it already has spock installed right? Well, one of my controller tests isn't working quite right. I'm trying to mock several of my services, but I keep getting this error:
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)
I couldn't really find anything on the internet about this. My controllerSpec looks like this:
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.... } }
My Service is even simpler:
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) } }
I am stomped. Even a grails clean wouldn't do the trick.. Any help? D: