Spock invocation for Groovy's generated getters using a final field

Viewed 2260

Groovy generates getters and setters for all your class' fields. So when you do this:

class Foo {
    final bar
}
new Foo().bar

you're actually calling the generated method Foo.getBar().

I have a Spock specification that likes to check the invocations of such a generated getter:

def "some spock test"() {
    given: def fooMock = Mock(Foo)
    when:  someFunction(fooMock)
    then:  1 * fooMock.getBar()
}

someFunction() does fooMock.bar but I always get

Too few invocations for:
1 * fooMock.getBar()   (0 invocations)

1 * fooMock.bar doesn't work, either. How can I check that bar is read from Foo in the test? It works, if I omit final, but this is a crappy solution...

1 Answers
Related