Spock unit test, groovy left shift assignment throws SpockExecutionException: data provider has no data
I am writing a spock unit test and I am getting the following error when trying to dynamically provide a data provider with groovy collect
SpockExecutionException: Data provider has no data
Here is the simplest case I can provide that throws the error:
import spock.lang.Shared
import spock.lang.Specification
class SampleTest extends Specification {
@Shared
def someArray
void setup() {
someArray = ['a','b','c']
}
def "ensure that 'Data provider has no data' is not thrown"() {
expect:
columnA == columnB
where:
[columnA, columnB] << someArray.collect { value -> [value, value] }
}
}
The groovy code seems to be working. Here's my test in groovy console:
def someArray = ['a','b','c']
def test = someArray.collect { value -> [value, value] }
println test
[[a, a], [b, b], [c, c]]
What? I do not understand?
I use:
- groovy version 2.2.1
- spock version 0.7- groovy -2.0
- junit version 4.12
+3
source to share
1 answer
Use setupSpec()
instead setup()
to access the variable the @Shared
way you want, as stated in the @Shared
documentation . Alternatively, you can also initialize a shared variable at the time of declaration.
import spock.lang.*
class SampleTest extends Specification {
@Shared someArray
// This is similar to just using
// @Shared someArray = ['a','b','c']
// Use above instead of setupSpec() if required
// setupSpec() is invoked before any test case is invoked
void setupSpec() {
someArray = ['a','b','c']
}
def "ensure that 'Data provider has no data' is not thrown"() {
expect:
columnA == columnB
where:
[columnA, columnB] << someArray.collect { [it, it] }
}
}
+6
source to share