I am trying to mock a Scala singleton object. In particular, I need to mock the object play.api.libs.ws.WS
used inside a service component (class under test).
U
Why you don't use the cake pattern?
It's a little verbose but it will solve your mocking problem.
trait GeolocationServiceComponent {
val geolocationService:GeolocationService
trait GeolocationService {
def wsClient
def getPath(origin:Location, destination: Location): Future[Route]
}
}
trait GeolocationServiceComponentImpl {
val geolocationService = new GeolocationService {
override def wsClient = WS
}
}
class DefaultGeolocationService extends GeolocationServiceComponent ...
//Defined into your test class
trait MockGeolocationServiceComponent {
val geolocationService = Mock[GeolocationService]
//Here you define your mock logic
}
You can also use monads to do this, but I have never implemented, it's described here: Scala dependency injection: alternatives to implicit parameters
I don't like to change the design to accommodate this kind of limitation. basically what I can get is all to change the design and then use the mock framework
//since mocking frameworks on SCALA is not support mocking singleton
//hacks are either required to change the design or ...
//I'd rather putting the mocking logic here by myself
var testUser:Option[User] = None;
def getCurrentUser(request: Request[Object]) = {
if( testUser != None ){
testUser;
}
hope it helps.
Mocking singleton objects using ScalaMock 3 is not possible, however Paul Butcher expects to reintroduce this feature in ScalaMock 4 (see http://paulbutcher.com/2014/04/15/scalamock-status-report/)
Don't mock the singleton. Instead of WS, make your service component depend on a thin facade hiding it:
trait GeolocationService {
def ws: (String, Seq[String]) => Promise[Response] = { (url, params) =>
wsClient.url(serviceProviderEndpoint.get).withQueryString(params: _*).get()
}
def getPath(origin: Location, destination: Location): Future[Route]
}
and in your test, just override ws method with a mock, which is now easy to create:
val mockedWs = mock[(String, Seq[String]) => Promise[Response]]
// TODO specify mock's behavior here
val service = new DefaultGeolocationService() {
override def ws = mockedWs
}