Spock 可以监视真实对象吗?
Can Spock spy for real object?
我想监视 Spring bean 的方法调用。我检查了 docs - Spock 只能通过构造函数创建间谍。 Spock 可以通过间谍包装已经存在的对象吗?
好像做不到,因为API不支持。看看 API。以下代码运行错误:
@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def 'test'() {
given:
def o = new Object()
def s = Spy(o)
}
}
来自官方Spock framework documentation:
You may also create a spy from an instantiated object. This may be useful in cases where you do not have full control over the instatiation of types you are interested in spying. (For example when testing within a Dependency Injection framework such as Spring or Guice.)
我想监视 Spring bean 的方法调用。我检查了 docs - Spock 只能通过构造函数创建间谍。 Spock 可以通过间谍包装已经存在的对象吗?
好像做不到,因为API不支持。看看 API。以下代码运行错误:
@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def 'test'() {
given:
def o = new Object()
def s = Spy(o)
}
}
来自官方Spock framework documentation:
You may also create a spy from an instantiated object. This may be useful in cases where you do not have full control over the instatiation of types you are interested in spying. (For example when testing within a Dependency Injection framework such as Spring or Guice.)