我有一个Scala项目,我正在使用mockito (mockito-core 3.0)进行测试。
下面是我试图模拟的函数的函数签名
def hset[V: ByteStringSerializer](key: String, field: String, value: V): Future[Boolean] = ...
这不管用
verify(mockObj, never()).hset(anyString(), anyString(), anyLong())
错误退出与此
Invalid use of argument matchers!
4 matchers expected, 3 recorded:
不知道当函数有3个带有泛型类型的参数时,它为什么需要4个匹配器?
这行得通
verify(mockObj, never()).hset("a", "b", 3.0)
这是因为我使用的scala代码不能正确地使用mockito内核?
发布于 2019-11-14 00:27:52
造成这个问题的原因是上下文约束
def hset[V: ByteStringSerializer](key: String, field: String, value: V): Future[Boolean]
实际上是
def hset[V](key: String, field: String, value: V)(implicit ev: ByteStringSerializer[V]): Future[Boolean]
现在,您可以看到为什么有4个参数,请尝试
verify(mockObj, never()).hset(anyString(), anyString(), anyLong())(any(classOf[ByteStringSerializer[Long]]))
发布于 2019-11-14 00:42:01
正如Ivan指出的,你错过了隐含的匹配器。我建议您迁移到mockito-scala,因为当隐式在作用域时,这类场景就会开箱即用。
https://stackoverflow.com/questions/58852068
复制