Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spocklight: Reuse Variables in Data Providers

DZone's Guide to

Spocklight: Reuse Variables in Data Providers

This trick for writing parameterized specifications in Spock involves specifying different values from your data providers for efficient testing.

· Java Zone ·
Free Resource

Bring content to any platform with the open-source BloomReach CMS. Try for free.

Writing a parameterized specification in Spock is easy. We need to add the where: block and use data providers to specify different values for each set of values from the data providers our specifications are run, so we can test for example very effectively with multiple input arguments for a method and the expected outcome.

A data provider can be anything that implements the Iterable interface. Spock also adds support for a data table. In the data table, we define columns for each variable and in the rows values for each variable. Since Spock 1.1, we can reuse the value of the variables inside the data provider or data table. The value of the variable can only be reused in variables that are defined after the variable we want to reuse is defined.

In the following example, we have two feature methods: one uses a data provider and one a data table. The variable sentence is defined after the variable search, so we can use the search variable value in the definition of the sentence variable.

package mrhaki
 
import spock.lang.Specification
import spock.lang.Unroll
 
class CountOccurrencesSpec extends Specification {
 
    @Unroll('#sentence should have #count occurrences of #search (using data table)')
    void 'count occurrences of text using data table'() {
        expect:
        sentence.count(search) == count
 
        where:
        search  | sentence                                                  || count
        'ABC'   | "A simple $search"                                        || 1
        'Spock' | "Don't confuse $search framework, with the other $search" || 2
    }
 
    @Unroll('#sentence should have #count occurrences of #search (using data provider)')
    void 'count occurrences of text using data provider'() {
        expect:
        sentence.count(search) == count
 
        where:
        search << ['ABC', 'Spock']
        sentence << ["A simple $search", "Don't confuse $search framework, with the other $search"]
        count << [1, 2]
    }
}


When we run the specification, the feature methods will pass, and in the @Unroll descriptions, we see that the sentence variable uses the value of search:

Written with Spock 1.1-groovy-2.4.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
java ,java testing ,spock ,data providers ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}