1

Unit test:

class OtherServiceSpec extends Specification {
    def fooCacheService = Mock(FooCacheService)
    ...
}

Service:

class FooCacheService extends CacheService<String> {
    ...
}

Generic base class:

class CacheService<T> {
    ...
}

Stack trace:

java.lang.IllegalArgumentException
    at net.sf.cglib.proxy.BridgeMethodResolver.resolveAll(BridgeMethodResolver.java:61)
    at net.sf.cglib.proxy.Enhancer.emitMethods(Enhancer.java:911)
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:498)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
    at org.spockframework.mock.runtime.ProxyBasedMockFactory$CglibMockFactory.createMock(ProxyBasedMockFactory.java:91)
    at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:49)
    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:45)
    at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:281)
    at org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:99)
    at com.bsb.site.OtherServiceSpec.$spock_initializeFields(OtherServiceSpec.groovy:2)
Kevan Ahlquist
  • 5,375
  • 1
  • 17
  • 25

1 Answers1

0

I don't get any explanation why this happens. I just have observed that Mock() gets a problem when mocking an implementation of an interface.

I solved this problem by mocking the interface. So in your case, you should build your mock with this command:

def cacheService = Mock(CacheService)

This of course also means that you have also to implement your class in question (OtherService) by using the interface CacheService and not FooCacheService.

Hope this helps.

Cheers Oliver

Oliver
  • 1