How to use instance variable in where section of Spock Test
Asked Answered
B

1

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 ?

Blamable answered 28/3, 2014 at 7:59 Comment(3)
do you have a code sample of what you're trying to do?Esteban
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.Eduino
@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 .Blamable
W
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.

Walrus answered 11/5, 2014 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.