ps0604
ps0604

Reputation: 1071

Mockito: Ambiguous reference to overloaded definition

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?

Upvotes: 4

Views: 5556

Answers (1)

Dima
Dima

Reputation: 40510

You can't use mockito directly like this (you can use it, but can't make it look this pretty). Take a look at scala test library. The simplest thinng you can do to solve your immediate problem with it is just mix in MockitoSugar into your test class instead of importing Mockito._, then mock[Foo] will just work as you expect it to. There are many other things that library offers to write idiomatic test code in scala, so you should read through some docs and examples on that site I linked to.

Upvotes: 4

Related Questions