Scala Mockito:对重载定义的不明确引用
鉴于以下代码使用Mockito模拟Scala类,我得到一个错误,无法编译:Scala Mockito:对重载定义的不明确引用,scala,mockito,Scala,Mockito,鉴于以下代码使用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.fun
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
}
}
import org.mockito.mockito_
可测试类{
def fun1=1
def fun2=2
}
对象测试{
def getMock={
val testMock=mock[testable]/你不能像这样直接使用mockito(你可以使用它,但不能让它看起来这么漂亮)。看一看。
要解决当前的问题,最简单的方法就是将MockitoSugar
混合到测试类中,而不是导入Mockito.\u
,然后mock[Foo]
将按照您的预期工作。
库还提供了许多其他东西来用scala编写惯用测试代码,所以您应该阅读我链接到的网站上的一些文档和示例。mock(classOf[testable])
?它需要一个java.lang.Class
,而classOf
方法提供了它,就像在java中你可以做testable.Class
。更正,ClassAny
有一个getClass
方法,所以对于像Int
,String
等非泛型类,你可以使用getClass
和gen code>eric的作品,比如List[T]
你使用classOf[List[\u]]]
来自Predef
…谢谢,它使用mock(classOf[testable])
@insan-e模仿Int
听起来是个好主意!:)使用mock(classOf[testable])
似乎有效。想法?在Mockito
中有不同的mock
重载风格。您以前使用的参数化参数需要两个参数。一个只接受一个类
参数的参数使用后一种语法。不确定是什么让您不满意。问题是scala不能处理signatu这是一个更大的问题,间谍/doReturn返回一个AnyValscenario@AndrewNorman不,这与省略号无关。另外,scala处理得很好。@Dima在以下代码段中可以很容易地识别scala省略号问题:val myBool=false class MyClass(){def booleanstest():Boolean={false}}val mySpy=spy(新MyClass)doReturn(myBool).when(mySpy).booleanstest编译错误:对重载定义的引用不明确,类型为(x$1:Any,x$2:object*)的对象Mockito中的两个方法doReturnorg.mockito.stubing.Stubber和类型(x$1:Any)为org.mockito.stubing.Stubber的对象mockito中的方法doReturn匹配参数类型(布尔值)