Reputation: 3
I'm new with Mockito and I'm dealing with a very strange problem with matchers
def fetchById[T: Format](elasticDetails: ESDetails): F[ESResult[T]]
I have this def for my Elastic search client and the problem start with the generic Format : T that i have there to pass.
.fetchById[JsValue](anyObject[ESDetails])
.returns(IO.pure(ESResult(200, value = Some(fakeResponse)))) ```
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at org.specs2.mock.mockito.MockitoMatchers.anyObject(MockitoMatchers.scala:49)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));```
//This the error for matchers that i'm getting after I run my tests.
Upvotes: 0
Views: 581
Reputation: 15465
You are implicitly passing a real instance of Format[T]
alongside the fake Matcher
instance of ESDetails
. Mockito requires that all arguments be either real instances or Matchers and does not allow you to mix them, as you see from this error message.
The simplest fix will be to turn the implicit argument into a Matcher, e.g.
.fetchById[JsValue](anyObject[ESDetails])(eq(implicitly[Format[T]]))
See also What precisely is a scala evidence parameter and How to stub a method call with an implicit matcher in Mockito and Scala
As mentioned by Mateusz, you will probably be better off using scalamock than Mockito, as it handles this situation much better as it was designed for Scala.
Upvotes: 1