I wanna spy method calls of Spring bean. I checked docs - Spock can create spy only by constructor. Can Spock wrap already existing object by spy?
Can Spock spy for real object?
If I'm not mistaken you can't do this because it creates a proxy, i.e., it creates a derived type on the fly. –
Barely
It seems that it can't be done because of the fact that API doesn't support it. Have a look at API. The following piece of code runs with errors:
@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)
}
}
This may no longer be the case. From the 1.1 release notes: "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.)" –
Fanfani
For creating a spy from an instantiated object like @StefanvandenAkker quoted^, if anyone can post an example, thank you. –
Afterbirth
From the official 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.)
If anyone can post an example, thank you. –
Afterbirth
© 2022 - 2024 — McMap. All rights reserved.