Mock parameter type constraints do not work for primitive types #669

ddimtirov opened this Issue Oct 26, 2016 · 1 comment


None yet

2 participants


Issue description

When we define a stub or mock on an API using method overloads differing by their primitive types, the argument type constraint needs to be specified as boxed type.

How to reproduce

Consider the interface:

interface Foo {
  String bar(long x);
  String bar(double x);

The following mock will throw an error:

def foo = Mock(Foo) {
  _ * bar(_ as long) >> "Long"
  _ * bar(_ as double) >> "Double"
  0 * _

If we specify the constraints as boxed - it works:

def foo = Mock(Foo) {
  _ * bar(_ as Long) >> "Long"
  _ * bar(_ as Double) >> "Double"
  0 * _

But then what about this Java interface?

interface Foo {
  String bar(long x);
  String bar(Long x);

Additional Environment information

Spock 1.0



Groovy version



Spock does nothing with it. Groovy does all the magic.
From the official groovy doc:

While Groovy declares and stores primitive fields and variables as primitives, because it uses Objects for everything, it autowraps references to primitives.

Another one example related to your issue -

So it's not a bug, it's a feature :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment