Mockito doReturn: ambiguous reference to overloaded definition
Asked Answered
C

5

19

I'm trying to port a Scala system to Mockito2. There are a few test cases that use doReturn and now in Mockito 2.18.0 I get this error:

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)

Looking in Mockito.java, doReturn is really overloaded like that:

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

How on Earth is this not always ambiguous? How do I make it compile?

Thanks

Crispin answered 30/5, 2018 at 8:45 Comment(0)
C
11

There is a ticket in the Scala backlog on it. see https://github.com/scala/bug/issues/4775

Curvy answered 21/6, 2018 at 12:0 Comment(0)
J
21

As a temporary workaround, you can do the following:

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

Then have your test mixin this MockitoHelper.

Jerz answered 13/12, 2018 at 15:56 Comment(3)
That's exactly what mockito-scala does, but is already done for you in the MockitoSugar traitAletheaalethia
I don't think you would need extends MockitoSugar here. overriding doReturn would be sufficient. Correct me if I am wrong :)Alltime
For me, it was enough to replace doReturn by Mockito.doReturn(toBeReturned, Nil: _*).Lohengrin
K
13

This can also be overcome by using doAnswer instead of doReturn

// no good
doReturn(true).when(foo).bar()
// works
doAnswer(_ => true).when(foo).bar()
Kehoe answered 4/12, 2019 at 23:39 Comment(0)
C
11

There is a ticket in the Scala backlog on it. see https://github.com/scala/bug/issues/4775

Curvy answered 21/6, 2018 at 12:0 Comment(0)
A
2

This is a bit of self promotion but I just published a library called mockito-scala that solves this issue and many more, is part of the mockito ecosystem so hopefully should become the default when working with Scala, you can find it here https://github.com/mockito/mockito-scala with the information to get the dependency and what problems does it actually solves.

Specifically for your problem, you could write this code and it would work out of the box

doReturn(Future.successful(())).when(f.adapterSpy).myFunction(userData, Some(offerId), Always)

I changed the way the future is expressed just because is the correct way to create a completed future of Unit

Aletheaalethia answered 23/7, 2018 at 17:12 Comment(5)
Hello, that would be better to explain how this library would solve the problem, with code included (have a look to stackoverflow.com/help/how-to-answer)Wortham
@baptistemm Good point, I didn't do it here cause the exact same code that doesn't work for the OP would work out of box with this library, also the Github page link explains all the other features as wellAletheaalethia
We prefer to have the code to be on answer to avoid loss of info if the reference goes away for instance.Wortham
@baptistemm fixed!Aletheaalethia
@BaptisteMille-Mathias sorry to be pedantic, but Future.unit is legit ... i think the capitalization of Unit was the only issue in the question :) also, thanks for the tip here — it solves my varargs issue w/ scala!Meed
W
0

Just adding Nil: _* as the second arg did it for me (credits to @Darwin)

doReturn( 
  Future.successful(None), 
  Nil: _*  //avoid ambiguous reference to overloaded definition
).when(f.adapterSpy).myFunction(userData, Some(offerId), Always)
Whitmire answered 20/6 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.