org.specs2.mock.Mockito matchers are not working as expected

我是研究僧i 提交于 2019-12-06 16:02:10

When you are using matchers to match parameters you have to use them for all parameters. as the all arguments have to be provided by matchers indicates.

Moreover if you use a specs2 matcher it needs to be strongly-typed. equals is a Matcher[Any] but there is no conversion from Matcher[Any] to a String which is what method accepts.

So you need a Matcher[T] or a Matcher[String] in your case. If you just want to test for equality, the strongly-typed matcher is ===

there was no(logger).error(any[Exception], ===("hey"))

I would like to add that you should be wary of default arguments, i.e. if using matchers when stubbing methods, make sure to pass argument matchers for all arguments, because default arguments will almost certainly have constant values - causing this same error to appear.

E.g. to stub the method

def myMethod(arg1: String, arg2: String arg3: String = "default"): String

you cannot simply do

def myMethod(anyString, anyString) returns "some value"

but you also need to pass an argument matcher for the default value, like so:

def myMethod(anyString, anyString, anyString) returns "some value"

Just lost half an hour figuring this out :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!