Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mockito with scala: matchers issue

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading