Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 38 Β· Testing with Spock

Data-Driven Testing with where: in Spock

Run one Spock feature against many inputs with where: data tables, data pipes, multi-variable pipes, computed data, readable test names and database-style sources.

Tests for pure logic usually repeat the same shape with different numbers: "given 2 and 3, expect 5; given -1 and 1, expect 0". Copying the feature for each pair is noisy. Spock's where: block runs one feature once per row of data, reports each row as its own test, and keeps the data in a table that is easy to read, extend and review.

Spock and Groovy cannot run on this site. The Groovy fences are read-only. The Java stand-ins implement the same row-by-row idea with arrays and records.

Data tables

GROOVY
import spock.lang.Specification

class MaxSpec extends Specification {

  def "max of #a and #b is #expected"() {
    expect:
    Math.max(a, b) == expected

    where:
    a  | b  || expected
    1  | 3  || 3
    7  | 4  || 7
    0  | 0  || 0
    -5 | -2 || -2
  }
}

The first row is a header naming the data variables. Each following row is one test case. Values are separated by |, and a double bar || visually separates inputs from expected outputs (it makes no difference to Spock).

  • Data variables (a, b, expected) are available in every block of the feature, no declaration needed.
  • #a, #b in the feature name are replaced with each row's values, so the report shows max of 1 and 3 is 3. That makes failures easy to find.
  • Tables need at least two columns. For one variable use a pipe (below).
  • All rows run even when one fails, and each row is reported separately.

Data pipes

When a variable comes from a collection, use <<:

GROOVY
def "a string's length is not negative"() {
  expect:
  s.length() >= 0

  where:
  s << ["", "a", "hello", "ΓΌnΓ―code"]
}

Anything iterable works: lists, ranges, results of a method, even a file reader:

GROOVY
where:
n << (1..5)
word << new File("words.txt").readLines()

A multi-variable pipe takes a list of lists, with an underscore to ignore a column:

GROOVY
where:
[a, b, _] << [[1, 2, "x"], [3, 4, "y"]]

Pipes with different lengths run until the shortest is exhausted, unless you combine them as a cartesian product yourself.

Computed variables

Right-hand sides can be expressions that use other data variables:

GROOVY
where:
a | b
1 | 2
5 | 6
sum = a + b            // assignment, computed per row

You can also mix a table with a derived variable c = a * b below it. Use this for expected values that are easy to derive, but not as a copy of the code under test, because the test would then prove nothing.

Mixing tables and pipes

GROOVY
where:
user << ["ann", "bob"]
role << ["admin", "guest"]       // pairs: (ann, admin) and (bob, guest)

For all combinations, build the product explicitly with [xs, ys].combinations() in a helper and pipe that list.

Testing exceptions per row

GROOVY
def "parsing #text fails with #message"() {
  when:
  Integer.parseInt(text)

  then:
  def e = thrown(NumberFormatException)
  e.message.contains(fragment)

  where:
  text  | fragment
  "abc" | "abc"
  ""    | "empty"
}

The thrown() call runs per row, as does every assertion.

Unrolling and naming

Modern Spock versions unroll where: features automatically, which gives each row its own entry in the report. Control the name with placeholders #a, #b, or with #a.name (property access) and #obj.method() (method call). Plain text like "max of #a and #b is #expected" documents the case better than a bare "feature 1".

Use @Rollup on a feature to collapse the iterations into one reported test, useful for huge tables.

Good table design

  1. Put the inputs on the left, expected results on the right.
  2. Add a short description column for tricky rows: "leap year" | 2000 || true.
  3. Include boundary values (0, 1, maximum, empty, null) and a few failing cases.
  4. Keep tables under about 20 rows. For larger data, use a pipe from a file.
  5. Do not repeat business logic in the expected column.

Try it: a data table in Java (stand-in)

The program runs one check against each row and prints a line named from the row, like the unrolled report.

Java Stand-in: one feature, many rows

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Try it: a cartesian product with pipes (stand-in)

All combinations of two lists, like feeding [xs, ys].combinations() into a pipe.

Java Stand-in: every combination

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. What does each row of a where: data table produce?

    Spock runs the whole feature once per row and reports each iteration.
  2. 2. What is the operator << used for in a where: block?

    var << iterable pipes each element of the collection in as the variable's value.
  3. 3. How do you show a row's values in the test name?

    Placeholders like #a are substituted per iteration, which makes failing rows easy to identify.
  4. 4. What happens if one row fails?

    Each iteration is independent, so you see every failing row at once.

Summary

  • where: runs one feature for each data row.
  • Tables use | and ||; pipes use << with any iterable.
  • Data variables need no declaration and can appear in the feature name via #var.
  • Computed variables, exception checks and files as data sources all work.
  • Good tables are short, readable and include edge cases.

Next: Mocking and Stubbing in Spock