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: solidsoft.wordpress.com 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)" () {
    when:
    println("a=${a} b=${b}")
    then:
    (...)
    where:
    [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)" () {
    when:
    println("a = ${a} b=${b}")
    then:
    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 com.ofg.loans.domain.model.ledger.acl.RequestUtilsSpec.should 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 comment:

  1. Thanks for share that knowledge with others!

    Btw, there are already slides available with my (similar) presentation from 4developers conference: https://speakerdeck.com/szpak/smarter-testing-java-code-with-spock-framework

    ReplyDelete