spock icon indicating copy to clipboard operation
spock copied to clipboard

MissingMethodException when accessing fields without explicit `@`

Open kriegaex opened this issue 4 years ago • 1 comments

Describe the bug

In order write this Stack Overflow answer, I implemented a test demonstrating data pipes. It works nicely on Spock 1.3. Then I copied it into GWC, and it started failing, complaining about missing getters in lines using field access. Then same happens locally in the latest Spock version:

event.id > 0
|     |
|     groovy.lang.MissingMethodException: No signature of method: de.scrum_master.stackoverflow.q71631373.FirstEvent.getId() is applicable for argument types: () values: []
|     Possible solutions: getAt(java.lang.String), wait(), init(), find(), grep(), wait(long)
|     	at de.scrum_master.stackoverflow.q71631373.DynamicBaseClassTest.basic event class functionality(DynamicBaseClassTest.groovy:18)
de.scrum_master.stackoverflow.q71631373.FirstEvent$SpockMock$1692978949@3e595da3

To Reproduce

This GWC example works. Now simply remove the @ characters.

    // '.id' and '.name' should be enough, but on Spock 2.1 there is a problem
    // when not explicitly using the '@' notation for direct field access.
    event.@id > 0
    [email protected]() == 10

Expected behavior

The test passes without the .@id field access notation under Spock 2.x, just like under 1.3.

Actual behavior

Spock complains about missing getters.

Java version

17

Buildtool version

Maven

What operating system are you using

Windows

Dependencies

x

Additional context

x

kriegaex avatar Mar 27 '22 02:03 kriegaex

Looking at the example code, I see that the fields are protected. If you make them public, it works without the .@field access.

https://groovy-lang.org/objectorientation.html#properties

You can define a property with:

  • an absent access modifier (no public, protected or private)

leonard84 avatar Jun 29 '22 17:06 leonard84

#1729 would fix this issue.

AndreasTu avatar Jul 31 '23 10:07 AndreasTu

Was fixed by #1729

AndreasTu avatar Sep 15 '23 17:09 AndreasTu