Skip to content

Spock with Strong Types #1279

@Richargh

Description

@Richargh

Issue description

Is there a way to create data-driven tests with Spock that use explicit or implicit type conversion?

In my sample project I have classes like Age or Name that wrap Integer and String plus a Person { Name name; Age age; } that uses them. I'd like to write my data like so:

def "a test"(Name name, Age age){
    where:
    name      | age
    "John"    | 5
    "Lisa"     | 2
}

or like this:

def "a test"(Person person){
    where:
    name      | age
    "John"    | 5
    "Lisa"     | 2
}

What I don't want to do is to write it like this, because that creates a lot of noise in the test:

def "a test"(Name name, Age age){
    where:
    name                   | age
    Name.of("John") | new Age(5)
    Name.of("Lisa")  | new Age(2)
}

I've written a Groovy type coercion extension method from String to Person but Spock does not want to use it.

JUnit5 automatically uses a static factory for type conversion or I can supply an ArgumentConverter/ArgumentAggregator. Can I do something similar in Spock?

How to reproduce

Link to a gist or similar (optional)

I created a sample test if you want to take a look.

Additional Environment information

  • Java 11
  • Spock: "2.0-M4-groovy-3.0"
  • Groovy: "3.0.7"
  • Gradle 6.8.2
  • Mac OS X

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions