I'd like to override the getProperty()
method in a Spock test, per the documentation of get/setProperty. This is trivial in a normal Groovy class, but doesn't seem to work inside a Spock specification.
class MainSpec extends Specification {
@Override
def getProperty(String name) {
def value = metaClass.getProperty(this, name)
println "$name == $value"
return value
}
String foo = 'foo'
def test() {
expect:
foo
println foo
}
}
This example does not invoke the getProperty()
method. It appears Spock is bypassing it somehow. Is there a way to hook into Spock's property resolution mechanism, or tell Spock to use my overridden method?
It turns out that moving the property to a parent class is sufficient to make it pass through the getProperty()
interceptor, even when the parent is another Specification
class.
class BaseSpec extends Specification {
def foo = 'foo'
}
class MainSpec extends BaseSpec {
@Override
def getProperty(String name) {
def value = super.getProperty(name)
println "$name == $value"
return value
}
def test() {
expect:
foo
println foo
}
}