fedor.belov
fedor.belov

Reputation: 23343

Can Spock spy for real object?

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?

Upvotes: 6

Views: 3671

Answers (2)

A. Masson
A. Masson

Reputation: 2477

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

Upvotes: 2

Opal
Opal

Reputation: 84854

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

Upvotes: 3

Related Questions