https://kotlinlang.org logo
#mockk
Title
# mockk
t

tim

09/01/2021, 12:58 PM
Hi is there a way to mock the properties of a singleton? For example given:
Copy code
object Singleton {
  val client = SomeClient()
   fun foo() {
    // calls to me are mocked in my unit test
    client.bar()
  }
}
When I mock calls to
foo
with
mockkObject
using
every
the SomeClient() constructor is still called. So do I also need to mock SomeClient, or is there a way I can mockkAll of Singleton or perhaps just intercept calls to client?
m

Mattia Tommasone

09/01/2021, 12:59 PM
i think you can do this by mocking the constructor for
SomeClient
, doing something like
Copy code
every { anyConstructed<SomeClient>().bar() }
does this make sense to you?
t

tim

09/01/2021, 1:19 PM
Ya i think so ... and if SomeClient were to also be a singleton? 😂
I guess i'll need to find the concrete class in the dependency graph and mock it there 🤔
2 Views