Module override not working for instrumented tests
Describe the bug
Override of restclient module doesn't not work for instrumented test.
To Reproduce
-
Download repo-branch
-
Run instrumented test
CarPoolViewTest -
Logs will show that real API is being called.
-
Removing
module(override = true)atCarPoolViewTestshowsDefinitionOverrideExceptionsince module is already delcared. -
Break pointing
RestClientatCarPoolRepositoryImplit's possible to see that module was not override, actually the one being loaded is from production code atInjectorclass.
Expected behavior
HttpClientStubshould replaceCoreHttpClientImplin order to mock API calls
Koin project used and used version
koin-androidx-viewmodel 2.0.1
koin-test version 2.0.1
@Before definition, where overriden module is loaded, was being called after repository calls API.
Tried approach using
@Rule
@JvmField
val rule = object : ActivityTestRule<NavigatorActivity>(
NavigatorActivity::class.java
) {
override fun beforeActivityLaunched() {
super.beforeActivityLaunched()
val rest = module {
factory<CoreHttpClient>(override = true) {
HttpClientStub(get()).also { stub ->
single<RestClient>(override = true) { RestClientImpl(stub) }
}
}
}
loadKoinModules(rest)
}
}
Even though now Koin points its bind before activity starts and calls API , problem persists.
I/TestRunner: started: test(CarPoolViewTest) I/MonitoringInstr: Activities that are still in CREATED to STOPPED: 0 W/ActivityTestRule: getActivityIntent() returned null using default: Intent(Intent.ACTION_MAIN) I/[Koin]: bind type:'com.stonetree.restclient.feature.httpclient.CoreHttpClient' ~ [type:Factory,primary_type:'com.stonetree.restclient.feature.httpclient.CoreHttpClient'] total 13 registered definitions load modules in 0.110633 ms D/LifecycleMonitor: Lifecycle status change: com.stonetree.freemoving.NavigatorActivity@d76f192 in: PRE_ON_CREATE D/LifecycleMonitor: Lifecycle status change: com.stonetree.freemoving.NavigatorActivity@d76f192 in: CREATED I/art: Do full code cache collection, code=124KB, data=111KB I/art: Starting a blocking GC JitCodeCache After code cache collection, code=80KB, data=60KB D/LifecycleMonitor: Lifecycle status change: com.stonetree.freemoving.NavigatorActivity@d76f192 in: STARTED D/LifecycleMonitor: Lifecycle status change: com.stonetree.freemoving.NavigatorActivity@d76f192 in: RESUMED D/OkHttp: --> GET https://mydomain/path --> END GET
I will keep issue open until further solution or possible evaluation for a Koin bug.
After further development, it was possible to conclude that the issue is not related to koin. Module's override feature is working properly.
i have same problem then what is your solution?
I have same problem here 😢
same problem. single(override=true) {} was working well. Maybe you can define same function on koin test library?