Mockito doReturn:对重载定义的模糊引用

2022-09-03 17:07:50

我正在尝试将Scala系统移植到Mockito2。有一些测试用例可以使用,现在在Mockito中我得到这个错误:doReturn2.18.0

Error:(34, 5) ambiguous reference to overloaded definition,
both method doReturn in object Mockito of type (x$1: Any, x$2: Object*)org.mockito.stubbing.Stubber
and  method doReturn in object Mockito of type (x$1: Any)org.mockito.stubbing.Stubber
match argument types (com.twitter.util.Future[Unit])
doReturn(Future.Unit).when(f.adapterSpy).myFunction(userData, Some(offerId), Always)

查看 ,真的是像这样超载:Mockito.javadoReturn

public static Stubber doReturn(Object toBeReturned) 
public static Stubber doReturn(Object toBeReturned, Object... toBeReturnedNext)

这到底是怎么并不总是模棱两可的?如何编译它?

谢谢


答案 1

作为临时解决方法,您可以执行以下操作:

trait MockitoHelper extends MockitoSugar {
  def doReturn(toBeReturned: Any): Stubber = {
    Mockito.doReturn(toBeReturned, Nil: _*)
  }
}

然后让你的测试混合在这个.MockitoHelper


答案 2

这也可以通过使用而不是doAnswerdoReturn

// no good
doReturn(true).when(foo).bar()
// works
doAnswer(_ => true).when(foo).bar()

推荐