当我为mock参数传递任何字符串时,InvalidUseOfMatchersException

时间:2016-11-02 19:29:03

标签: scala mockito

我希望我的模拟能够处理方法的任何输入

val redis = mock[RedisClient]
when(redis.scard(any[String])).thenReturn(Some("hello"))

错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers!
[info] 2 matchers expected, 1 recorded:
[info] -> at ..(SomeSpec.scala:123)
[info] 
[info] This exception may occur if matchers are combined with raw values:
[info]     //incorrect:
[info]     someMethod(anyObject(), "raw String");

1 个答案:

答案 0 :(得分:1)

scard有两个参数,其中one parameterimplicit

// SCARD
// Return the number of elements (the cardinality) of the Set at key.
def scard(key: Any)(implicit format: Format): Option[Long] =
  send("SCARD", List(key))(asLong)

如果你没有指定参数,Scala会提供一个参数,这会干扰Mockito line up matchers with arguments的能力。

另请参阅:org.specs2.mock.Mockito matchers are not working as expected