This question is an extension of the solved issue mentioned at https://github.com/paulbutcher/ScalaMock/issues/79
I have the following trait to mock:
trait HttpClient{
def deserialize[T](response: HttpResponse)
(implicit um: Unmarshaller[ResponseEntity, T],
executionContext: ExecutionContext): Future[T]
}
I'm trying to mock the HttpClient as follows
val client = mock[HttpClient]
case class SomeTypeT(name:String, id:Int)
implicit val someTypeTFormat = jsonFormat2(SomeTypeT) // to be able to marshal and unmarshal from JSON
(httpClient.deserialize[SomeTypeT](_: HttpResponse))
.expects(where {
(response: HttpResponse) => {
response.entity == ent
}
})
.returns(Unmarshal(response.entity).to[SomeTypeT])
The problem occurs when I try to mock the deserialize function. As above, the deserialize
method consists of a Typed Parameter T
and a single parameter of type HttpResponse
, and 2 more implicit parameters used while unmarshalling the response.
The question, therefore, is how to use ScalaMock to mock the deserialize
function and specify multiple implicit parameters while mocking. This does not work
// Both UnMarshaller & ExecutionContext are available here as implicits
(httpClient.deserialize[SomeTypeT](_: HttpResponse)(_: Unmarshaller[ResponseEntity, SomeTypeT](_: ExecutionContext))
The problem is I cannot use _
to specify both the implicit parameters. And I don't know how to achieve this. Please help how to mock the given function
I'm using the following libraries:
While the second attempt does not even compile due to multiple _
being used, the first one results in following exception:
org.scalamock.function.MockFunction3 cannot be cast to org.scalamock.function.MockFunction1
java.lang.ClassCastException: org.scalamock.function.MockFunction3 cannot be cast to org.scalamock.function.MockFunction1
Following the answer at GitHub that you linked, your code should be something like
(httpClient.deserialize[SomeTypeT](_: HttpResponse)(_: Unmarshaller[ResponseEntity, SomeTypeT], _:ExecutionContext))
.expects(where {
(response: HttpResponse, _: Unmarshaller[ResponseEntity, SomeTypeT], _:ExecutionContext) => {
response.entity == ent
}
})
.returns(Unmarshal(response.entity).to[SomeTypeT])
I.e. you explicitly put placeholders for implicit parameters in the call as a second group of parameters and in the where
as additional parameters after all non-implicit.