Created
September 3, 2019 07:09
-
-
Save mrolcsi/5b10fe6fd1c5822ac4634f660cb59e75 to your computer and use it in GitHub Desktop.
[WIP] Instrumented tests with Dagger injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://android.jlelse.eu/how-to-inject-mock-dependencies-into-android-components-using-dagger-androidinjector-e274c8f6a9a6 | |
class TestApplication : Application(), HasSupportFragmentInjector { | |
@Inject | |
lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment> | |
override fun supportFragmentInjector() = fragmentInjector | |
} | |
class CustomTestRunner : AndroidJUnitRunner() { | |
override fun newApplication(cl: ClassLoader?, className: String?, context: Context?): Application { | |
return super.newApplication(cl, TestApplication::class.java.name, context) | |
} | |
} | |
@Singleton | |
@Component(modules = [TestApplicationModule::class, FragmentModule::class, AndroidSupportInjectionModule::class]) | |
interface TestApplicationComponent : AndroidInjector<TestApplication> { | |
@Component.Builder | |
abstract class Builder : AndroidInjector.Builder<TestApplication>() { | |
abstract fun appModule(appModule: TestApplicationModule): Builder | |
} | |
} | |
@Module | |
class TestApplicationModule( | |
private val userRepository: UserRepository = FakeUserRepository(), | |
private val detailsRepository: DetailsRepository = FakeDetailsRepository() | |
) { | |
@Provides | |
@Singleton | |
fun provideUserRepository(): UserRepository { | |
return userRepository | |
} | |
@Provides | |
@Singleton | |
fun provideDetailsRepository(): DetailsRepository { | |
return detailsRepository | |
} | |
@Provides | |
@Singleton | |
fun provideSchedulerProvider(): SchedulerProvider = AppSchedulerProvider() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment