Mockito任何匹配器都不适用于具有重载方法的doAnswer [英] Mockito any matcher not working for doAnswer with overloaded method

查看:494
本文介绍了Mockito任何匹配器都不适用于具有重载方法的doAnswer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从具有2个重载的gradle类中删除一个void方法,并且mockito匹配器与worng方法匹配,而我的存根是failling。这是我到目前为止:

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))

但是当我调试调用来查看匹配器,它正在寻找方法 public abstract void org.gradle.api.Project.afterEvaluate(org.gradle.api.Action),而不是 public abstract void org.gradle.api.Project.afterEvaluate(groovy.lang.Closure)

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)

只要提到这是groovy不是Java,我不知道这是否有什么关系。

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

我已经检查了 any(Closure .class) matcher确实只是返回一个空值。假设演员应该调用正确的方法,但由于某种原因,它没有注册正确的方法。

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.

接受任何解决方法。

谢谢。

Thanks.

推荐答案

好的,我发现这个问题

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

显然是groovy的错因为即使 any(Closure.class)方法已经返回正确的类型,我也必须将null转换为正确的类型。如下所示:

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)

这篇关于Mockito任何匹配器都不适用于具有重载方法的doAnswer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆