Home > Net >  Testing with Manual Dependency Injection according to Google
Testing with Manual Dependency Injection according to Google

Time:02-10

There is an article from Google about Manual Dependency Injection. In it, they use an Application class that holds an AppContainer, which itself holds dependencies that other classes might need:

class AppContainer {
    private val retrofit = Retrofit.Builder()...
    val userRepository = UserRepository(localDataSource, remoteDataSource)
}

An Activity might then do the following to get a dependency, eg. a repository:

val appContainer = (application as MyApplication).appContainer
val loginViewModel = LoginViewModel(appContainer.userRepository)

They state the following:

Dependency injection ... enables you to swap out implementations for testing.

Can this be done with that approach (or any of the others presented on the page mentioned above)?

I tried to come up with a canonical and didn't find one that wouldn't involve reflection or mocking or anything of the sorts.

CodePudding user response:

  •  Tags:  
  • Related