Home > database >  mockito with scala: matchers issue
mockito with scala: matchers issue

Time:12-22

I have mocked a service. Service has a method 'action' which takes an object of type MyCustomObject and returns a Future of Either. So to mock:

when(myService.action(any[MyCustomObject]())).thenReturn(any[Future[Any]]())

I don't see any issue here conceptually. I am telling mockito to mock method action such that whenever it is called with any object of type MyCustomObject, then make it return a Future of Any as I don't care about value inside Future.

But it give me error:

When using matchers, all arguments have to be provided by matchers.

Both of my mocked values are generic in nature, so what's the cause of this error?

CodePudding user response:

When using Mockito, you cannot pass a matcher in the thenReturn: the point of mocking is to define arbitrary return values.

Conceptually, Mockito could probably generate some random data for simple types but how would you expect Mockito to generate data for a type it doesn't know at all and maybe accept only some specific values?

TL;DR: You have to provide a return value:

when(myService.action(any[MyCustomObject]()))
  .thenReturn(Future.succesfull(Right(something)))
  • Related