>我正在尝试用Mockito为Web服务编写一个模拟。模拟应使用播放 WS 库模拟 POST 请求。
/**
* Mock for the Web Service
*/
case class WSMock() extends Mockito {
val wsRequestHolder: play.api.libs.ws.WS.WSRequestHolder = mock[play.api.libs.ws.WS.WSRequestHolder]
val wsResponse: play.api.libs.ws.Response = mock[play.api.libs.ws.Response]
wsResponse.status returns 200
wsResponse.body returns "BODY RESP FROM WS"
val futureResponse = scala.concurrent.Future { wsResponse }
wsRequestHolder.post(any[Map[String,Seq[String]]]) returns futureResponse
}
运行测试时,出现以下错误:
[error] InvalidUseOfMatchersException:
[error] Invalid use of argument matchers!
[error] 3 matchers expected, 1 recorded:
[error] -> at org.specs2.mock.mockito.MockitoMatchers$class.any(MockitoMatchers.scala:24)
[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"));
[error]
[error] For more info see javadoc for Matchers class.
在我看来,使用复杂类型(具有嵌套类型参数)的any[...] 表达式无法正确解析为匹配器。但是,我看不出原始类型在哪里发挥作用。为参数Map[String,Seq[String]]
指定此类匹配器的正确方法是什么?
多谢!
wsRequestHolder.post(any[Map[String,Seq[String]]]) returns futureResponse
请注意,post
实际上那里有几个额外的隐式参数:
def post [T] (body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]):
Promise[Response]
。可能需要显式匹配,就像在这个 spec2 用户线程中一样。
似乎 wsRequestHolder.post 方法需要三个参数,因此 Mockito 希望您发送三个(例如 any[])匹配器,但您只为其中一个提供了匹配器。