12

Spock only allows static variables to be accessed from where block.

Is there any workaround using which Instance variables can be used inside the where block ?

MedicineMan
  • 15,008
  • 32
  • 101
  • 146
  • do you have a code sample of what you're trying to do? – brwngrldev Mar 28 '14 at 18:17
  • There is no workaround that allows to use instance variables in a where-block. There may be a way to solve your bigger goal, but you'd have to explain it. – Peter Niederwieser Mar 29 '14 at 06:43
  • @PeterNiederwieser - I was trying to declare an abstract getter of a field so that the derived class has to declare the variable, I want to access the fields of this variable in the where block . If I declare the variable in the derived class it doesnt work, however I found a workaround , I have not declared the variable in derived class but the getter and returned anonymous object . – Biraj B Choudhury Apr 01 '14 at 11:31

1 Answers1

14

You can annotate instance variables with @Shared, see Spock manual, chapter "Sharing of Objects between Iterations".

Attention: Shared instance variables will retain their values in between iterations and be shared between methods. If you do not want this, do not use the approach. But as I understand you, you initialise variable values using tables or so in the where block anyway, so it should be okay.

kriegaex
  • 63,017
  • 15
  • 111
  • 202