Reputation: 597
I have this example script that looks like this:
@Stepwise
class RandomTest extends GebReportingSpec {
@Shared
Random random = new Random()
def tag = random.nextInt(999)+1
def setupSpec() {
new File('/ProgramData/geb.properties').withInputStream {
properties.load(it)
}
}
def "Random Test"(){
when:
println("Random1: ${tag}")
println("Random2: ${tag}")
then:
Thread.sleep(1000)
}
def "Random test2"(){
when:
println("Random3: ${tag}")
then:
Thread.sleep(1000)
}
}
In this example, Random1, and Random 2 print the same number, but Random 3 prints a different number.
for example this is my output when I run the code:
Random1: 528
Random2: 528
Random3: 285
I assume that this is because the Shared variables are re-evaluated between feature methods. I have tried moving those variable declarations outside of the @Shared
annotation, to no avail.
I want it so that the random tag variable is generated at the beginning of the spec, and I want it to retain its value, but I am not sure how I would set up the global variables to do this. would I need to instantiate the variables inside of the setupSpec?
Upvotes: 0
Views: 492
Reputation: 2683
@Shared
variables are not re-evaluated between tests. The reason for your observation is that you output the not @Shared
variable tag
. random.nextInt(999)+1
is evaluated before each test method.
If you put a @Shared
on tag
the value won't change.
Upvotes: 2
Reputation: 597
Looks like insantiating the varaible from inside the setup spec is what works:
@Shared
def tag
def setupSpec() {
new File('/ProgramData/geb.properties').withInputStream {
properties.load(it)
}
Random random = new Random()
tag = random.nextInt(999)+1
}
def "Random Test"(){
when:
println("Random1: ${tag}")
println("Random2: ${tag}")
then:
Thread.sleep(1000)
}
def "Random test2"(){
when:
println("Random3: ${tag}")
then:
Thread.sleep(1000)
}
Upvotes: 0