Pick the fix
A ViewModel creates ArticleRepository(Retrofit.Builder()...build()) inside itself, and tests hit the real network. Best fix?
Hint 1 of 1
Who should create the repository?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Take the repository as a constructor parameter and provide it with DI
Correct. Tests can pass a fake, and the app shares one instance.
Why not the others?
- B Add an
isTestflag. Test flags leak into production code. - C Mock Retrofit's static builder. Fragile, and the design stays coupled.
- D Make the repository a global object. Still hard to replace in tests.