我有以下Java类:
public class FooServiceImpl {
private BarService barService;
public String generateFoo() {
String barValue = barService.generateBar();
return customFoo() + barValue;
}
public String customFoo() {
return "abc";
}
}
这里是示例 Spock 测试方法:
def "generate foo bar"() {
setup:
def barService = Mock(BarService) {
generateBar() >> "Bar"
}
FooServiceImpl spyFooService =
Spy(FooServiceImpl, constructorArgs: [[barService: barService]])
spyFooService.customFoo() >> "foo"
when:
def fooValue = spyFooService.generateFoo()
then:
fooValue == "fooBar"
}
我尝试为
FooServiceImpl
创建一个 Spy 对象上课,但我收到以下错误:org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack:
No such property: barService for class:
com.foo.FooServiceImpl$$EnhancerByCGL`
我无法向
FooServiceImpl
添加构造函数或 BarService
的二传手,所以我想使用 map 构造函数。这可能吗?注:根据this issue它应该工作
最佳答案
在您的情况下,最简单的解决方案是将此字段设为 protected
而不是 private
.当你从一个类创建一个 spy 对象时,CGLIB 被涉及,它从你试图创建 spy 的类创建为子类 - com.foo.FooServiceImpl$$EnhancerByCGL
在你的情况下。问题是您要修改的字段是私有(private)字段,并且根据 Java 中的常规子类化策略,私有(private)字段不会在子类中继承。这就是为什么字段 barService
spy 对象中不存在
ATTENTION: IntelliJ's debugger may tell you that
barService
is present in thisspyFromService
instance, however this is IDE's bug - if you list all available fields fromspyFromService.class.fields
orspyFromService.class.declaredFields
you wont findbarService
field here.
另一个问题是,当 CGLIB 参与对象创建过程时,如果涉及到调用方法,它也会参与其中。这就是为什么通过 Groovy 的元编程功能向类或实例添加动态字段不起作用的原因。否则,您将能够执行以下操作:
spyFromService.metaClass.barService = barService
或者
spyFromService.class.metaClass.barService = barService
或者,您可以摆脱 spy 对象并在测试中使用真实实例。然后
FooServiceImpl spyFromService = new FooServiceImpl()
spyFromService.@barService = barService
将工作。但是,您将无法 stub 现有的
customFoo()
方法,您将不得不依赖其实际实现返回的内容。
关于java - 如何使用私有(private)属性(property)创建 Spock Spy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818211/