0

I'm writing a unit test for a class that has the Play2 class DefaultSyncCacheApi as a dependency. I'm calling the method getOrElseUpdate[A: ClassTag](key: String, expiration: Duration)(orElse: => A): A from inside the class that I'm testing.

Of course, I don't want to test the caching class but I just want to mock it so I can test other functionality.

I tried using Mockito:

mockCache = mock[DefaultSyncCacheApi]    
when(mockCache.getOrElseUpdate(any(),any())(any())(any()).thenReturn(Future[String])

I started with any() in the first place, then I also tried:

  when(mockCache.getOrElseUpdate[List[Item]](any(),any())
  (any[List[Item]])
  (any[ClassTag[List[Item]]])
  ).thenReturn(List(new Item))

I always get the same error:

InvalidUseOfMatchersException: 
Invalid use of argument matchers!
4 matchers expected, 3 recorded:

So, in the end I'm not able to apply all 4 matchers from the method definition.

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
Robert Gabriel
  • 1,151
  • 12
  • 24
  • Possible duplicate of [Mocking a function with pass-by-name arguments](https://stackoverflow.com/questions/35948519/mocking-a-function-with-pass-by-name-arguments) – Artavazd Balayan Oct 30 '17 at 15:25
  • So should I use Scala Mock instead? In the end I created a TestClass implementation for the cache class but is like a workaround. – Robert Gabriel Oct 31 '17 at 00:53
  • Yes, you can go with ScalaMock, but even there the syntax [for describing call-by-name is terrible](https://stackoverflow.com/a/18298495/5794617). But if you're mocking not so many classes/traits which contain methods with call-by-name, you can go with your own mocks (fake implementations). – Artavazd Balayan Oct 31 '17 at 04:06
  • Yes, that looks nasty. I'll go with fake implementation for now. Thanks. – Robert Gabriel Oct 31 '17 at 05:05

0 Answers0