1

单元测试:

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

服务:

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

通用基类:

class CacheService<T> {
    ...
}

堆栈跟踪:

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

1 回答 1

0

我没有得到任何解释为什么会发生这种情况。我刚刚观察到 Mock() 在模拟接口的实现时会出现问题。

我通过模拟界面解决了这个问题。因此,在您的情况下,您应该使用以下命令构建您的模拟:

def cacheService = Mock(CacheService)

这当然也意味着您还必须使用接口CacheService而不是 FooCacheService来实现您的类( OtherService )。

希望这可以帮助。

干杯奥利弗

于 2016-03-09T13:44:00.060 回答