For a final
property, Groovy generates a final
getter method. However, test doubles created with Mock()
, Stub()
, or Spy()
are purely proxy-based, and therefore cannot intercept final methods.
Since your code under test is written in Groovy, you can use a GroovyMock()
instead, which solves the problem.
PS: Both 1 * foo.getBar()
and 1 * foo.bar
are valid notations.
PPS: Only prefer GroovyMock()
over Mock()
if you have a concrete reason (mocking a final method, mocking a dynamic method, etc.). For details, see the reference documentation.