单元测试:
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)
答案 0 :(得分:0)
我没有得到任何解释为什么会发生这种情况。我只是观察到Mock()在模拟接口的实现时遇到了问题。
我通过模拟界面解决了这个问题。所以在你的情况下,你应该用这个命令构建你的模拟:
def cacheService = Mock(CacheService)
这当然也意味着您还必须使用 CacheService 接口而不是FooCacheService来实现您所讨论的类( OtherService )。
希望这有帮助。
干杯 奥利弗