Reputation: 2811
this is how im mocking my service call and returning a fake result:
when(myService.doSomething("",fakeRequestAsModel)) thenReturn fakeResult
val result = call(controller.myActionMethod(), request)
the problem is in the controller method myActionMethod
when I call doSomething
and passing the arguments im calling some property that that will only return something in production...
def myActionMethod() ... = {
myService.doSomething(request.getSomeValue,requestAsModel)
...
}
so, getSomeValue
is a method i can call only in production, it comes with a 3rd party library and I cant override it.
How can I still mock this call so request.getSomeValue
wont throw me an exception?
and request.getSomeValue
is dynamic, I cant unfortunately put it in configuration...
Upvotes: 0
Views: 303
Reputation: 3921
// we rename this because Scala defines `eq` on `AnyRef`
import org.mockito.Matchers.{eq => eqMockito, _}
...
when(myService.doSomething(anyString(), eqMockito(fakeRequestAsModel)))
thenReturn fakeResult
Here we want Mockito to return this answer when any string is sent and exact fakeRequestAsModel
, which is what you want.
Notes:
any
matchers with normal values, you cant say:
when(myService.doSomething(anyString(), fakeRequestAsModel))
.eqMockito()
method.any[classOf[T]]
for type-parametrized arguments.Hope it helps!
Upvotes: 2