spockframework / spockframework/spock
Reference to field or property with explicit this in then block
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 483
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
Making a reference to the spec's field or property using explicit-this in the then block produces an STC error. No error shows for this.myField in the when block.
this.myField in the then is written as $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), this).myField in the feature method. record returns Object, so as far as STC is concerned, it can't find the property "myField" in java.lang.Object.
Spock 2.4
Groovy 5.0.5
To Reproduce
@groovy.transform.TypeChecked
class SomeSpec extends Specification {
String myField
void testField() {
when:
this.myField = 'test'
then:
this.myField == 'test' // [Static type checking] - No such property: myField for class: java.lang.Object
}
}
Expected behavior
this.myField checks properly or is exempted from STC in when, then, expect, etc.
Actual behavior
"[Static type checking] - No such property: myField for class: java.lang.Object"
Java version
Java 25.0.2
Buildtool version
Eclipse 2026-06 / Groovy 5.0.5
What operating system are you using
Windows
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproduced @TypeChecked specification in the issue and inspect how Spock transforms explicit this references in when and then blocks, especially the generated $spock_valueRecorder expression. Run the reproduction with Spock 2.4 and Groovy 5.0.5; done means the then-block reference no longer produces the reported STC error while the when-block behavior remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, java
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100