2

I'm trying to stub a void method from a gradle class that has 2 overloads and mockito matchers are matching the worng method and my stub is failling. Here is what I have so far:

doAnswer(new Answer<Void>() {
  @Override
  Void answer(final InvocationOnMock invocation) throws Throwable {
    Closure clo = invocation.arguments[0] as Closure
    clo.run()
    return null
  }
}).when(mProject).afterEvaluate(any(Closure.class))

But when I debugged the call to see the matcher, it was looking for the method public abstract void org.gradle.api.Project.afterEvaluate(org.gradle.api.Action) instead of public abstract void org.gradle.api.Project.afterEvaluate(groovy.lang.Closure)

Just mentioning that this is groovy not Java and I'm not sure if that has anything to do with it.

I have checked what does the any(Closure.class) matcher does ad it simply returns an null value. Supposedly the cast should make the call to the right method but for some reason it is not registering with the right method.

Any workaround accepted as well.

Thanks.

superjugy
  • 301
  • 4
  • 12

1 Answers1

4

Ok, I found this other question Unexpected behavior with overloaded methods

Apparently is groovy's fault as I have to cast the null to the correct type even if the any(Closure.class) method already returns the right type. like this:

doAnswer(new Answer<Void>() {
  @Override
  Void answer(final InvocationOnMock invocation) throws Throwable {
    Closure clo = invocation.arguments[0] as Closure
    clo.run()
    return null
  }
}).when(mProject).afterEvaluate(any(Closure.class) as Closure)
Community
  • 1
  • 1
superjugy
  • 301
  • 4
  • 12