Abdul R
Abdul R

Reputation: 43

Mockito argument matchers InvalidUseOfMatchersException for Scala Project

I have this Scala project that I am using mockito (mockito-core 3.0) to test

Here's the function signature of the function I am trying to mock

def hset[V: ByteStringSerializer](key: String, field: String, value: V): Future[Boolean] = ...

This doesnt work

verify(mockObj, never()).hset(anyString(), anyString(), anyLong())

Error outs with this

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

Not sure why its expecting 4 matchers when the function has 3 argument with a generics type

This works

verify(mockObj, never()).hset("a", "b", 3.0)

is this because I am using scala code which doesnt operate correctly with the mockito core ?

Upvotes: 1

Views: 199

Answers (2)

ultrasecr.eth
ultrasecr.eth

Reputation: 1497

As Ivan pointed out, you're missing the matcher for the implicit. I'd suggest you to migrate to mockito-scala as this kind of scenarios will work out-of-the-box when the implicit is in scope

Upvotes: 1

Ivan Stanislavciuc
Ivan Stanislavciuc

Reputation: 7275

The reason for the problem is context bound

def hset[V: ByteStringSerializer](key: String, field: String, value: V): Future[Boolean]

is actually

def hset[V](key: String, field: String, value: V)(implicit ev: ByteStringSerializer[V]): Future[Boolean]

Now you can see why there are 4 arguments, try

verify(mockObj, never()).hset(anyString(), anyString(), anyLong())(any(classOf[ByteStringSerializer[Long]]))

Upvotes: 1

Related Questions