Given the following code that mocks a Scala class with Mockito, I get an error and cannot compile:
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
}
}
Error is:
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 ?
I just mocked a class, what's ambiguous?
mock(classOf[Testeable])
? It expects ajava.lang.Class
, andclassOf
method provides it, just like in Java you could doTesteable.class
. – insan-eAny
has agetClass
method, so for non-generic classes likeInt
,String
etc you could usegetClass
and for generic ones likeList[T]
you useclassOf[List[_]]
fromPredef
... – insan-emock(classOf[Testeable])
– ps0604Int
sounds like a wonderful idea! :) – Dima