maloney
maloney

Reputation: 1653

Mocking a Future Or in a Scala Spec

I'm having trouble with Mockito to mock a response from a method that returns either an Object or an Exception. The mocked method's signature looks like this:

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

and in my Specs I'm trying to just return a succesful Future:

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

This of course does not compile but what is the correct syntax?

Upvotes: 4

Views: 5582

Answers (2)

rethab
rethab

Reputation: 8413

Well you need to decide what you want to return. Depending on the test, you may want to return the left or the right side of the Or.

Eg.

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

Upvotes: 7

Jegan
Jegan

Reputation: 1751

You cannot stub both on the same. But you can stub it as two different invocations like below.

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

We are stubbing "findResult" to return Future[Success] the first time and Future[Failure] the second time.

Upvotes: 1

Related Questions