Scala Spec2 Mockito: Argument matchers with complex types

泪湿孤枕 提交于 2020-01-13 23:29:34

问题


I'm trying to write a mock for a web service with Mockito. The mock should simulate a POST request using the play WS library.

/**
 * 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
}

When running the test I get the following error:

[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.

It looks to me as the any[...] expression using a complex type (with nested type parameters) does not correctly get resolved into a matcher. However, I don't see where the raw type comes into play. What is the proper way to specify such a matcher for a parameter Map[String,Seq[String]]?

Thanks a lot!


回答1:


wsRequestHolder.post(any[Map[String,Seq[String]]]) returns futureResponse

Note that post actually has a couple extra implicit parameters there:

def post [T] (body: T)(implicit wrt: Writeable[T], ct: ContentTypeOf[T]):
    Promise[Response]

...which probably need to be matched explicitly, as in this spec2-users thread.




回答2:


It seems the wsRequestHolder.post method requires three parameters, so Mockito expects you to send three (e.g. any[]) matchers, but you provided matchers for only one of them.



来源:https://stackoverflow.com/questions/19157783/scala-spec2-mockito-argument-matchers-with-complex-types

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