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

Global mocking not working when receiving null on methods with many signatures #871

Open
fogrizovic opened this Issue Aug 15, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@fogrizovic

fogrizovic commented Aug 15, 2018

Hi,

I'm trying to stub a static method from Java class, which has more than one signature. Like this

def "test stubbing"() {
       given:
       GroovySpy(Foo, global: true)
       Foo.generate(_) >>  "bla"
       when:
       def res = Foo.generate((Obj1) null)
       then:
       res == "bla"
   }

Foo is a java class with mehtod "generate" defined as:

public static String generate(Obj1 obj) {
// somehting
}

public static String generate(Obj2 obj) {
// somehting
}

Then I'm getting:

groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method Foo#generate.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[Obj1]
[Obj2]

Is this the expected behaviour? How can I call generate with "null" value?

@postapczuk

This comment has been minimized.

Show comment
Hide comment
@postapczuk

postapczuk Aug 17, 2018

You cannot stub method with two signatures in one stub. You need to precise which signature you are stubbing. Use Foo.generate(_ as Obj1) >> "bla1" and Foo.generate(_ as Obj2) >> " bla2" .

postapczuk commented Aug 17, 2018

You cannot stub method with two signatures in one stub. You need to precise which signature you are stubbing. Use Foo.generate(_ as Obj1) >> "bla1" and Foo.generate(_ as Obj2) >> " bla2" .

@fogrizovic

This comment has been minimized.

Show comment
Hide comment
@fogrizovic

fogrizovic Aug 17, 2018

Hi @postapczuk

I also tried that, and I'm still getting the same. Tried this:

given: 
GroovySpy(Foo, global: true)
Foo.generate(_ as Obj1) >>  "bla"
Foo.generate(_ as Obj2) >>  "ble"
when:
def res = Foo.generate((Obj1) null)

fogrizovic commented Aug 17, 2018

Hi @postapczuk

I also tried that, and I'm still getting the same. Tried this:

given: 
GroovySpy(Foo, global: true)
Foo.generate(_ as Obj1) >>  "bla"
Foo.generate(_ as Obj2) >>  "ble"
when:
def res = Foo.generate((Obj1) null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment