Reputation: 108
I need help with scalatest and mockito. I want to write test for a simple method with generic:
trait RestClient {
def put[T: Marshaller](url: String, data: T, query: Option[Map[String, String]] = None) : Future[HttpResponse]
}
my test class:
class MySpec extends ... with MockitoSugar .... {
...
val restClient = mock[RestClient]
...
"Some class" must {
"handle exception and print it" in {
when(restClient.put(anyString(), argThat(new SomeMatcher()), any[Option[Map[String, String]])).thenReturn(Future.failed(new Exception("Some exception")))
...
}
}
}
when I run test, it throws following exception:
Invalid use of argument matchers!
4 matchers expected, 3 recorded:
So, why it asks 4 matchers, if my method has only 3 parameters? Is it because of generic?
versions:
Upvotes: 1
Views: 445
Reputation: 15557
This is because the following notation
def put[T: Marshaller](a: A, b: B, c: C)
is equivalent to
def put[T](a: A, b: B, c: C)(implicit m: Marshaller[T])
So you need to pass in a matcher for the marshaller:
put(anyA, anyB, anyC)(any[Marshaller[T]])
Upvotes: 2