spockframework / spockframework/spock

Reference to field or property with explicit this in then block

Open
#2,343 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.