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

Open
ddimtirov opened this Issue Oct 26, 2016 · 1 comment

Projects

None yet

2 participants

@ddimtirov

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 * _
}

foo.bar(1L)

If we specify the constraints as boxed - it works:

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

foo.bar(1L)

But then what about this Java interface?

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

Additional Environment information

Spock 1.0

Java/JDK

1.8

Groovy version

2.4.7

@angry-cellophane
Contributor

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 - http://docs.groovy-lang.org/latest/html/documentation/core-differences-java.html#_primitives_and_wrappers.

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