我正在尝试用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.
在我看来,使用复杂类型(带有嵌套类型参数)的任何[...]表达式都无法正确地解析为匹配器。但是,我没有看到原始类型发挥作用的地方。
为参数Map[String,Seq[String]]
指定这样的匹配器的正确方法是什么?
非常感谢!
答案 0 :(得分:2)
wsRequestHolder.post(any[Map[String,Seq[String]]]) returns futureResponse
请注意,post
实际上有一些额外的隐式参数:
def post [T] (body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]):
Promise[Response]
...可能需要明确匹配,如this spec2-users thread。
答案 1 :(得分:0)
似乎wsRequestHolder.post方法需要三个参数,因此Mockito希望您发送三个(例如任何[])匹配器,但是您只为其中一个提供了匹配器。