-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spock with Strong Types #1279
Comments
Supporting the type coercion or rather why it is not supported is probably worth looking at. def "a test"(Name name, Age age) {
where:
theName | theAge
"John" | 5
"Lisa" | 2
name = Name.of(theName)
age = new Age(theAge)
} |
You could also write a global or annotation based extension that registers a feature method interceptor and adjusts as needed, like @MakePerson
def "a test"(Person person) {
println(person)
where:
name | age
"John" | 5
"Lisa" | 2
}
// ...
@Retention(RUNTIME)
@Target(METHOD)
@ExtensionAnnotation(MakePersonExtension)
@interface MakePerson {
}
class MakePersonExtension implements IAnnotationDrivenExtension<MakePerson> {
@Override
void visitFeatureAnnotation(MakePerson annotation, FeatureInfo feature) {
feature.featureMethod.addInterceptor(new IMethodInterceptor() {
@Override
void intercept(IMethodInvocation invocation) throws Throwable {
invocation.arguments[2] = person { it.withName(invocation.arguments[0]).withAge(invocation.arguments[1]) }
invocation.proceed()
}
})
}
} In reality you would probably do some magic like checking whether there is an unset |
Or a variant of the first version that also works: def "a test"(Name name, Age age) {
where:
theName | theAge
"John" | 5
"Lisa" | 2
name = theName as Name
age = theAge as Age
} |
And with my PR #1280 it will simply work as you expected. If you want to combine |
Alternatively, you could use a map if the object has a no args constructor, or add a custom |
Wow. Thank you so much for your fast response and I'm sorry for my tardiness when it comes to replying to the issue I created. Thanks for your replies. I've incorporated your code suggestions into my example project. I hope it's ok that I directly copied your extension code @Vampire and added an attribution? I eagerly look forward to the next Spock version that incorporates type coercion. |
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
orName
that wrapInteger
andString
plus aPerson { Name name; Age age; }
that uses them. I'd like to write my data like so:or like this:
What I don't want to do is to write it like this, because that creates a lot of noise in the test:
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
The text was updated successfully, but these errors were encountered: