Can't get this thing to work correctly.
class HelloInstrumentationTestRunner : AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?, className: String?, context: Context?
): Application {
return Instrumentation.newApplication(HelloTestApp::class.java, context)
}
}
startKoin {
androidLogger()
androidContext(applicationContext)
fragmentFactory()
modules(appModule + viewModelsModule)
}
stopKoin()
(says No Koin Context configured. Please use startKoin or koinApplication DSL)declareMock
in subsequent test methods are no longer working.All these problems are basically because application instance survives between tests, thus graph configured inside android application instance also survives between tests. I need that not to happen or at least have ability to modify graph between tests.
Solved.
val overrideModule = module(override = true) {
single<Repository1> {
mock(Repository1::class.java)
}
single { Repository2(get(), get()) }
single<Repository3> {
mock(Repository3::class.java)
}
...
}
loadKoinModules(overrideModule)
unloadKoinModules(overrideModule)
given(get<Repository1>().magicCall()).willReturn(
MagicData(
"1111",
Calendar.getInstance().timeInMillis
)
)
No need to deal with stopKoin and stuff like that, super easy!