Pick the fix
An object AnalyticsTracker holds a Context and an HTTP client, and tests can't replace it. What's the better design?
Hint 1 of 1
How would a test swap in a fake?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Make it a class behind an interface and provide one instance with DI as a singleton
Correct. Same lifetime, but injectable and replaceable in tests.
Why not the others?
- B Add a
reset()function for tests. Tests can still interfere with each other through shared global state. - C Make it a companion object. Still a global singleton.
- D Use lazy inside the object. Changes when it's created, not how it's replaced.