Reputation: 1948
I have a function within a class like:
def saveToken(token: Token, ttl: Instant, client: Client, partner: Partner, info: Info): Future[EitherErrorsOr[Done]]
and EitherErrorsOr
is :
type EitherErrorsOr[A] = scala.Either[Errors, A]
Errors
is our internal Errors
class
When I try to mock saveToken
as follows:
when(
mockService.saveToken(
any(), any(), any(), any(), any()
)
).thenReturn(Right(NoOpVal).toFut)
then I get an error such as:
overloaded method value thenReturn with alternatives:
(x$1: scala.concurrent.Future[EitherErrorsOr[Done]],x$2: scala.concurrent.Future[EitherErrorsOr[Done]]*)org.mockito.stubbing.OngoingStubbing[scala.concurrent.Future[EitherErrorsOr[Done]]] <and>
(x$1: scala.concurrent.Future[EitherErrorsOr[Done]])org.mockito.stubbing.OngoingStubbing[scala.concurrent.Future[EitherErrorsOr[Done]]]
cannot be applied to (scala.concurrent.Future[scala.util.Right[Nothing,NoOp]])
).thenReturn(Right(NoOpVal).toFut)
Why does thenReturn
come up with so many alternatives?
Note:
Done
is our internal class that signifies that an operation is complete ,
toFut
converts to a Future
object, NoOpVal
is just some type created for testing purposes
Upvotes: 1
Views: 1033
Reputation: 8539
The issue you are having is in the return type. The return type of the method is Future[EitherErrorsOr[Done]]
, which is Future[Either[Errors, Done]]
.
Let's now analyze the type of Right(NoOpVal).toFut
. The implementation of Right
is:
final case class Right[+A, +B](b: B) extends Either[A, B] {
def isLeft = false
def isRight = true
}
Let's assume that NoOpVal
is of type A
, when calling Right(NoOpVal)
you get an instance of type Right[Nothing, A]
, because you didn't supply the first generics to Right
.
Right[Nothing, A]
cannot be casted into Future[EitherErrorsOr[Done]]
.
How do you solve that? Easy just do:
when(mockService.saveToken(any(), any(), any(), any(), any()))
.thenReturn(Right[Errors, Done](NoOpVal).toFut)
and make sure NoOpVal
extends Done
.
Upvotes: 2