Question: Suppose I am testing a method which has a dependency on a (imported) singleton instance called WS (Web Service), which has a method, *url(url: String)*, which takes a URL and returns a request. 

```
def doRequest(url: String): Future[Response] = {
  val request = WS.url(url)
  for {
    response <- request.post(params)
  } yield {
    val res: JsResult[MyResult] = response.json.validate[MyResult]
    res.getOrElse(throw new NotSupportedException)
  }
}

```

I would like to be able to inject the WS dependency such that my unit tests do not require an actual outbound http request, but can instead depend on a mock WS instance. 

This has been a challenge for me because, while a singleton technically *does* have a type (Class[WS.type]), the WS's properties and methods are lost when binding the singleton to a val which expects a Class[WS.type]. This means that I cannot simply use a simple cake pattern, like so:

```
trait WSComponent {
  val ws: Class[_ <: WS.type]
}

object ApplicationContext extends WSComponent {
  val ws = WS
}

object TestContext extends WSComponent {
  val ws = mock[WS]
}

```

If I do this, and then make calls to WS's methods in either context, I get a compilation error, which is that Class[\_ <: WS.type] has no method called (for example) url().

For what seems like a similar reason (basically, singleton objects don't have types--even though they do--), I cannot provide an implicit parameter which takes a WS.type, because, again, I will lose the methods and properties that were declared on the singleton object.

What approaches are there for injecting dependencies on singleton objects? I enjoy using the cake pattern for DI, but it introduces quite a bit of boilerplate to my code, so the ideal solution would not compound the code with too much more boilerplate.

Thank you in advance for any suggestions.

Answer: 
Singleton object do have types, and you can call methods on them:

```
scala> val i: Int.type = Int
i: Int.type = object scala.Int

scala> i.box(42)
res0: Integer = 42

```

I am guessing your error has to do with 

```
val ws: Class[_ <: WS.type]

```

being implemented with:

```
val ws = WS

```

That cannot compile, and indeed a `Class[...]` has no `url()` method either. You can just type `ws` to `WS.type`:

```
trait WSComponent {
  val ws: WS.type
}

```

And change the mocking to `mock[WS.type]`.

---

*Edit*: the other way below only works if you have control over the `WS` type (apparently not the case here since it comes from play)

If you really want to avoid the singleton type, you could turn `WS` into a trait with a singleton implementation, and only refer to the trait in your cake.

```
trait WS {
  def url(url: String): Request
}

object SingletonWS extends WS {
  def url(url: String) = ??? // actual implementation
}

```

And in your cake:

```
trait WSComponent {
  val ws: WS
}

object ApplicationContext extends WSComponent {
  val ws = SingletonWS
}

object TestContext extends WSComponent {
  val ws = mock[WS]
}

```
You could try defining a trait that contains the calls from `WS` that you use and then a simple wrapper impl that delegates to `WS`. Something like this:

```
trait WSMethods{
  def url(str:String):Request
}

object WSWrapper extends WSMethods{
  def url(str:String) = WS.url(str)
}

```

Then use it like so in a trait that you mix into classes that need it:

```
trait WSClient{
  val ws:WSMethods
}

```

Once you do that it's more mockable. It's a bit cumbersome, but that's the way it's going to be when the object does not mix in a trait that defines its operations. If the people at typesafe had done that with `WS` then it would be easier to mock. Also, most mock frameworks (probably all) will barf if you try something along the lines:

```
val m = mock[WS.type]

```