Chris Evans

ceva24.dev | Thoughts, code and everything in-between

Spock: Interaction Testing with GroovyInterceptable

2014-06-09

Grails uses Spock as its testing framework by default. Spock can be a fantastically expressive framework, but I came across a stumbling block recently when trying to test a class with interceptable methods.

GroovyInterceptable is an interface that acts in a similar manner to Groovy proxies – any time a method is called on an object of a class that implements it, it reroutes through the invokeMethod() method in GroovyObject, which can be overridden to determine the appropriate action.

Unfortunately this means for Spock interaction testing, the code below will return a ‘too few invocations’ error message.

expect: 1 * myMockInterceptedObject.hello(*_) >> 'hello world'

In order to determine that the correct method is being invoked, instead the following has to be done:

expect: 1 * myMockInterceptedObject.invokeMethod('hello', *_) >> 'hello world'
Chris Evans profile picture
CHRIS EVANS
Senior Engineering Manager at the LEGO Group, UK