In this project, I want to demo how Koin works with UnitTest.
I got a ComponentA, which injects the ComponetB and works inside the App.
val appModule = module {
factory { ComponentA(get()) }
factory { ComponentD(get(named("default"))) }
factory<ComponentInterface> { ComponentB() }
factory<ComponentInterface>(named("default")) { ComponentB() }
}
This definition shows Koin how it should inject the dependency in the Applications
For some reason, I wish ComponentA could use ComponetC in the UnitTest, but ComponetD Still uses ComponentB.
And Here is how it should be:
private val mockModule = module {
single<ComponentInterface> { ComponentC() }
}
@get:Rule
val koinTestRule = KoinTestRule.create {
printLogger()
modules(appModule, mockModule)
}
private val componentA: ComponentA by inject()
private val componentD: ComponentD by inject()
You can check it in ExampleUnitTest, which extends the test class that KoinTest must extend if you want to use Koin in a unit test. You can see that the mockModule overrides the appModule, and it works as I expected.