I have a Scala class like this:
object MyClient {
private lazy val theClient: TheClient = new TheClient()
}
class MyClient {
import MyClient._
var client = null // this is only for unittest
def getSomethingFromTheClient() = {
if (client == null) client = theClient
client.getSomething() + " from MyClient"
}
}
Some of the code are only there to facilitate unittest, where I can mock TheClient and inject it to MyClient, like this (I am using Mockito):
val mockTheClient = mock[TheClient]
val testMyClient = new MyClient()
testMyClient.client = mockTheClient
testMyClient.getSomethingFromTheClient() shouldBe "blabla"
This works but seems ugly. Ideally if I can inject mockTheClient to the companion object field that would be great. Or am I missing something else?