We often face a scenario where we have to run the same piece of test code over and over with different data. Spock’s data-driven testing is the answer for it. Let’s take a very common scenario of testing an action that creates the User record. This action takes username, firstName, lastName and other properties. Our testing code in Spock would look like:

void "test create"() {
when:
        controller.params['username'] = username
        controller.params['firstName'] = firstName
        controller.params['lastName'] = lastName
        controller.params['password'] = password
        controller.params['email'] = email

        controller.save()

        then:
        User.list().last().username == username
}

That’s the bare-bone code for testing but how we are supposed to supply the data repetitively? Let’s add the Spock’s data-driven testing support to supply data in an iterative fashion. We do that with “where:” clause at the end of the test method like:

void "test create"() {
        when:
        controller.params['username'] = username
        controller.params['firstName'] = firstName
        controller.params['lastName'] = lastName
        controller.params['password'] = password
        controller.params['email'] = email

        controller.save()

        then:
        User.list().last().username == username

        where:
        username  | firstName | lastName | password | email
        'apandey' | 'Amit'    | 'Pandey' | 'p'      | 'pguptaATjellyfishtechnologies.com'
        'neeraj'  | 'Neeraj'  | 'Bhatt'  | 'p'      | 'neerajATjellyfishtechnologies.com'
        'vivek'   | 'Vivek'   | 'Yadav'  | 'p'      | 'vivekATjellyfishtechnologies.com'
        'manish'  | 'Manish'  | 'Bharti' | 'p'      | 'manishATjellyfishtechnologies.com'
        'vsadh'   | 'Vivek'   | 'Sadh'   | 'p'      | 'vsadhATjellyfishtechnologies.com'
    }

The data table you see in the end holds the variables and their values which would be supplied in the when: and then: clauses one-by-one. That’s it. You have used the Spock’s data-driven support. But there’s one glitch to it. If you look at the generated report of this test, you’ll find that only one test case is generated for this test case that has created 5 users. Seems unfair, isn’t it? Here comes “Unroll” feature to the rescue. If you annotate the test method with @Unroll.

@Unroll
    void "test create"() {
    // test code
}

Now, one test case will be created to report one iteration of the data. And the report would look like:
Unrolled

Is it only me or you also think it could have rocked if we could replace those weird numbers with something more readable? Well, say no more, you can add placeholder to show the values in the test-case’s title. You will have to edit the name to something like:

@Unroll
    void "test create for username: #username"() {
    // testing code
}

You can add placeholders for the test case variables by escaping them with #. And this would generate the report that would look like:
Unroll Readable

You can use more placeholder to make your test cases more readable in the report. That’s all to it. Hope, this helps you. 🙂