DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Spocklight: Reuse Variables in Data Providers

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.

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Oct. 05, 17 · Java Zone · Tutorial
Like (1)
Save
Tweet
5.84K Views

Join the DZone community and get the full member experience.

Join 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.

Data (computing)

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Making Machine Learning More Accessible for Application Developers
  • Cloud-Based Integrations vs. On-Premise Models
  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo