mardi 4 août 2015

Mocking Play WSRequestHolder get using Scalamock

I'm trying to test the following line of code using ScalaTest and ScalaMock.

val responseFuture = wsClient.url(url).withQueryString(params: _*).get()

wsClient type is THttpClient, which is a wrapper of play.api.libs.ws.WS.

Given that:

val mockHttpClient = mock[THttpClient]

is properly injected into my class under test, the test code is something like this:

val expectedUrl = "some url"
val mockRequestHolder = mock[WSRequestHolder]
inSequence {
  (mockHttpClient.url _).expects(expectedUrl).returns(mockRequestHolder)
  (mockRequestHolder.withQueryString _).expects(where {
    (parameters: Seq[(String, String)]) => {
      // assertions on parameters
      // ...
      true
    }
  }).returns(mockRequestHolder)

  val stubResponse = stub[WSResponse]
  val jsonBody = "{}"
  (stubResponse.json _).when().returns(Json.parse(jsonBody))
  (mockRequestHolder.get _).expects().returns(Future(stubResponse))
}

IntelliJ is highlighting mockRequestHolder.get as an error saying: cannot resolve symbol get. Nevertheless I'm able to run the test but the mock is clearly not working, and I'm getting: java.util.NoSuchElementException: JsError.get.

The mock is working when I try to mock any other method of WSRequestHolder, but not with method get.

Is this a ScalaMock bug or am I doing something wrong?

Aucun commentaire:

Enregistrer un commentaire