Sunday, April 19, 2015

Spock even more advanced

On Fridays we have technological meetings in our company. Everyone can prepare presentation and "test" this presentation in friendly environment. This time Marcin ZajÄ…czkowski - from blog: was presenting his brand new "Advanced Spock". If you work with Spock on daily basis you should attend this presentation (even if you don't feel you need the »advanced« part).
During presentation I was curious what will happen in "where:" section when we pass two arrays with different length and how to cycle over values to test every combination of parameters. Marcin told me he wants to read answers on my blog. Challenge accepted:
First things first, but not necessarily in that order - I will start with combinations. How to test all combinations of parameters?
@Unrolldef "should return combinations pair (#a,#b)" () {
    println("a=${a} b=${b}")
    [a,b] << [[1,2,3],[8,9]].combinations()
This test will end up with:
a=1 b=8
a=2 b=8
a=3 b=8
a=1 b=9
a=2 b=9
a=3 b=9
Now original problem - what will happen when you pass two arrays of different size?
@Unrolldef "should return combinations pair (#a,#b)" () {
    println("a = ${a} b=${b}")
    1==1    where:
    a << [1,2,3]
    b << [8,9]
a = 1 b=8
a = 2 b=9

org.spockframework.runtime.SpockExecutionException: Data provider for variable 'b' has fewer values than previous data provider(s)
at return combinations pair (#a,#b)(RequestUtilsSpec.groovy:60)

This error doesn't leave us any hope - we can't pass two arrays of different length.


  1. Thanks for share that knowledge with others!

    Btw, there are already slides available with my (similar) presentation from 4developers conference:

  2. Hi

    For anyone reading this looking for answers, it is because Spock doesn't invoke the test with a Cartesian set of {a,b}, but tries to merge the two arrays into larger rows of parameters

    a << [1, 2, 3]
    b << [7, 8, 9]

    would yield the following invocations:
    (1, 7)
    (2, 8)
    (3, 9)

    It looks like the author was expecting 6 invocations instead:
    (1, 8)
    (1, 9)
    (2, 8)
    (2, 9)
    (3, 8)
    (3, 9)