scredis / scredis

Non-blocking, ultra-fast Scala Redis client built on top of Akka IO.

Home Page:https://scredis.github.io/scredis/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to mock calls with matchers?

noah10 opened this issue · comments

I've run into a bit of a problem trying to use mockito to mock calls using matchers. If, for example, I do this:

val redisMock = mock[Redis]
when(redisMock.zAdd(anyString(), any[Map[String, Score]]).thenReturn(Future.successful(1L))

...then mockito will complain that it expected me to use 3 matchers but that I only used 2. The problem is that it wants me to use a matcher for the implicit Writer parameter as well. Simply adding

implicit val sw: Writer[String] = any[Writer[String]]

...to my code doesn't help; it then triggers an ArgumentsAreDifferent error:

[info]   org.mockito.exceptions.verification.junit.ArgumentsAreDifferent: Argument(s) are different! Wanted:
[info] redis.zAdd(
[info]     "head_zset",
[info]     Map(valid record|BN|104914 -> 0.0),
[info]     null
[info] );
[info] -> at FileTypeHeadingSpec$$anonfun$1.apply$mcV$sp(HeadingSpec.scala:53)
[info] Actual invocation has different arguments:
[info] redis.zAdd(
[info]     "head_zset",
[info]     Map(valid record|BN|104914 -> 0.0),
[info]     scredis.serialization.UTF8StringWriter$@7bb4c0d7
[info] );

The mockito folks don't seem to be very familiar with scala, so I thought I'd check and see if anyone here had any suggestions.

Well, this turned out to be one of those cases where you find the answer as soon as you post the question. The solution (which seems obvious now, of course) is to explicitly pass the expected implicit parameter using a matcher:

when(redisMock.zAdd(anyString(), any[Map[String, Score]])(any[Writer[String]])).thenReturn...