鉴于以下使用 Mockito 模拟 Scala 类的代码,我收到错误并且无法编译:
import org.mockito.Mockito._
class Testeable {
def fun1 = 1
def fun2 = 2
}
object test {
def getMock = {
val testMock = mock[Testeable] // <-- this line throws the error
when(testMock.fun1).thenReturn(3)
testMock
}
}
错误是:
ambiguous reference to overloaded definition, both method mock in object Mockito of type (x$1: Class[common.Testeable], x$2: org.mockito.MockSettings)common.Testeable and method mock in object Mockito of type (x$1: Class[common.Testeable], x$2: org.mockito.stubbing.Answer[_])common.Testeable match expected type ?
我刚刚模拟了一个类,有什么歧义吗?
最佳答案
你不能像这样直接使用mockito(你可以使用它,但不能让它看起来这么漂亮)。看看scala test library 。
要解决眼前的问题,最简单的方法就是将 MockitoSugar
混合到您的测试类中,而不是导入 Mockito._
,然后 mock[Foo ]
将按照您的预期工作。
该库还提供了许多其他功能来在 scala 中编写惯用的测试代码,因此您应该阅读我链接到的该网站上的一些文档和示例。
关于scala - Mockito:对重载定义的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337468/