Scala thenReturn重载方法有其他选择-如何解决这个问题?

Scala thenReturn重载方法有其他选择-如何解决这个问题?,scala,unit-testing,mockito,scalatest,Scala,Unit Testing,Mockito,Scalatest,我在类中有一个函数,如: def saveToken(token: Token, ttl: Instant, client: Client, partner: Partner, info: Info): Future[EitherErrorsOr[Done]] 而错误或是: type EitherErrorsOr[A] = scala.Either[Errors, A] Errors是我们的内部Errors类 当我尝试模拟saveToken时,如下所示: when( mockSe

我在类中有一个函数,如:

def saveToken(token: Token, ttl: Instant, client: Client, partner: Partner, info: Info): Future[EitherErrorsOr[Done]]
错误或
是:

type EitherErrorsOr[A] = scala.Either[Errors, A]
Errors
是我们的内部
Errors

当我尝试模拟
saveToken
时,如下所示:

when(
      mockService.saveToken(
        any(), any(), any(), any(), any()
    )
  ).thenReturn(Right(NoOpVal).toFut)
然后我得到一个错误,例如:

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)
重载方法值,然后返回可选值:
(x$1:scala.concurrent.Future[eithererror[Done]],x$2:scala.concurrent.Future[eithererror[Done]*)org.mockito.stubing.ongoingstubing[scala.concurrent.Future[eithererror[Done]]
(x$1:scala.concurrent.Future[eithererror[Done]])org.mockito.stubing.ongoingstubing[scala.concurrent.Future[eithererror[Done]]
无法应用于(scala.concurrent.Future[scala.util.Right[Nothing,NoOp]])
).然后返回(右(NoOpVal).toFut)
为什么
thenReturn
会想出这么多备选方案

注:
Done
是我们的内部类,表示操作已完成,
toFut
转换为
Future
对象,
NoOpVal
只是为测试目的创建的某种类型

您遇到的问题是返回类型。该方法的返回类型为
Future[eithererror[Done]]
,即
Future[Errors,Done]]

现在让我们分析一下
Right(NoOpVal).toFut的类型。实施的是:

final case class Right[+A,+B](B:B)扩展了[A,B]{
def isLeft=false
def isRight=true
}
假设
NoOpVal
A
类型,当调用
Right(NoOpVal)
时,您会得到一个
Right[Nothing,A]
类型的实例,因为您没有向
Right
提供第一个泛型

Right[Nothing,A]
不能转换为
Future[eithererror[Done]]

你如何解决这个问题?简单易行:

when(mockService.saveToken(any(),any(),any(),any(),any())
.thenReturn(右[错误,完成](NoOpVal.toFut)
并确保
NoOpVal
扩展
Done