How do I specify a Range instead of List in the 'where:' block of a Spock specification

StackOverflow https://stackoverflow.com/questions/21175836

  •  28-09-2022
  •  | 
  •  

Question

The following example code:

class MySpec extends spock.lang.Specification {
    def "My test"(int b) {
        given:
        def a = 1

        expect:
        b > a

        where:
        b << 2..4
    }
}

throws the following compilation error: "where-blocks may only contain parameterizations (e.g. 'salary << [1000, 5000, 9000]; salaryk = salary / 1000')"

but using a List instead of a Range:

        where:
        b << [2,3,4]

compiles and runs fine as expected.

Could I also specify a Range somehow?

Was it helpful?

Solution

Use

where:
b << (2..4)

The test can be optimized as below as well. Note no arguments to the test.

def "My test"() {
  expect:
  b > a

  where:
  a = 1
  b << (2..4)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top