org.specs2.mock.Mockito 匹配器未按预期工作
org.specs2.mock.Mockito matchers are not working as expected
这是我正在尝试的代码 运行:
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
import akka.event.LoggingAdapter
class MySpec extends Specification with Mockito {
"Something" should {
"do something" in new Scope {
val logger = mock[LoggingAdapter]
val myVar = new MyClassTakingLogger(logger)
myVar.doSth()
there was no(logger).error(any[Exception], "my err msg")
}
}
}
当 运行 执行此操作时,出现以下错误:
[error] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[error] Invalid use of argument matchers!
[error] 2 matchers expected, 1 recorded:
[error] -> at org.specs2.mock.mockito.MockitoMatchers$class.any(MockitoMatchers.scala:47)
[error]
[error] This exception may occur if matchers are combined with raw values:
[error] //incorrect:
[error] someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error] //correct:
[error] someMethod(anyObject(), eq("String by matcher"));
这很有意义,但是 eq("my err msg")
和 equals("my err msg")
都没有完成这项工作,因为我收到了一个错误。我错过了什么?
当您使用匹配器来匹配参数时,您必须将它们用于 所有 参数。正如 all arguments have to be provided by matchers
所示。
此外,如果您使用 specs2
匹配器,则它需要是强类型的。 equals
是 Matcher[Any]
但没有从 Matcher[Any]
到 String
的转换,这是 method
接受的。
因此您需要 Matcher[T]
或 Matcher[String]
。如果您只想测试相等性,强类型匹配器是 ===
there was no(logger).error(any[Exception], ===("hey"))
我想补充一点,您应该警惕 默认参数 ,即如果在存根方法时使用匹配器,请确保为 all 传递参数匹配器 个参数,因为默认参数几乎肯定会具有常量值 - 导致出现同样的错误。
例如存根方法
def myMethod(arg1: String, arg2: String arg3: String = "default"): String
你不能简单地做
def myMethod(anyString, anyString) returns "some value"
但您还需要为默认值传递参数匹配器,如下所示:
def myMethod(anyString, anyString, anyString) returns "some value"
刚刚花了半个小时才弄明白:)
这是我正在尝试的代码 运行:
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
import akka.event.LoggingAdapter
class MySpec extends Specification with Mockito {
"Something" should {
"do something" in new Scope {
val logger = mock[LoggingAdapter]
val myVar = new MyClassTakingLogger(logger)
myVar.doSth()
there was no(logger).error(any[Exception], "my err msg")
}
}
}
当 运行 执行此操作时,出现以下错误:
[error] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[error] Invalid use of argument matchers!
[error] 2 matchers expected, 1 recorded:
[error] -> at org.specs2.mock.mockito.MockitoMatchers$class.any(MockitoMatchers.scala:47)
[error]
[error] This exception may occur if matchers are combined with raw values:
[error] //incorrect:
[error] someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error] //correct:
[error] someMethod(anyObject(), eq("String by matcher"));
这很有意义,但是 eq("my err msg")
和 equals("my err msg")
都没有完成这项工作,因为我收到了一个错误。我错过了什么?
当您使用匹配器来匹配参数时,您必须将它们用于 所有 参数。正如 all arguments have to be provided by matchers
所示。
此外,如果您使用 specs2
匹配器,则它需要是强类型的。 equals
是 Matcher[Any]
但没有从 Matcher[Any]
到 String
的转换,这是 method
接受的。
因此您需要 Matcher[T]
或 Matcher[String]
。如果您只想测试相等性,强类型匹配器是 ===
there was no(logger).error(any[Exception], ===("hey"))
我想补充一点,您应该警惕 默认参数 ,即如果在存根方法时使用匹配器,请确保为 all 传递参数匹配器 个参数,因为默认参数几乎肯定会具有常量值 - 导致出现同样的错误。
例如存根方法
def myMethod(arg1: String, arg2: String arg3: String = "default"): String
你不能简单地做
def myMethod(anyString, anyString) returns "some value"
但您还需要为默认值传递参数匹配器,如下所示:
def myMethod(anyString, anyString, anyString) returns "some value"
刚刚花了半个小时才弄明白:)