2016-11-07 54 views
1

遇到麻煩與的Mockito嘲笑從返回任一種ObjectException的方法的響應。嘲笑的方法的簽名是這樣的:懲戒期貨或在Scala的規格

def findResult(request: String): Future[Seq[String] Or MyException] =

,並在我的功能我想只返回一個成功的Future

when(client.findResult("1234")) thenReturn Future.successful[Seq[String] Or MyException](Seq("Hello"))

這當然不能編譯,但什麼是正確的語法?

+0

那麼你需要決定你想要返回什麼。根據測試,您可能想要返回「Or」的左側或右側。例如。 'doReturn(Future.successful(SEQ( 「你好」)))。當(客戶端).findResult( 「1234」)' – rethab

+0

@rethab尼斯一個,謝謝!如果你把這個作爲一個答案,我會接受... – maloney

+0

你去:d – rethab

回答

2

那麼你需要決定要返回什麼。根據測試,你可能需要返回左邊或Or的右側。

例如,

doReturn(Future.successful(Seq("hello"))).when(client).findR‌​esult("1234")

1

你不能在同一個存根兩者。但是你可以將它作爲下面兩個不同的調用來存儲。

when(client.findResult("1234")).thenReturn(Future.successful(Seq("test"))).thenReturn(Future.failed(new MyException())) 

我們磕碰「findResult」回到未來[成功]第一次與未來[失效]第二次。

+0

這是否實際工作?嘗試編譯時遇到類型不匹配錯誤 – maloney