koin icon indicating copy to clipboard operation
koin copied to clipboard

Module override not working for instrumented tests

Open roliveiravictor opened this issue 7 years ago • 5 comments

Describe the bug

Override of restclient module doesn't not work for instrumented test.

To Reproduce

  1. Download repo-branch

  2. Run instrumented test CarPoolViewTest

  3. Logs will show that real API is being called.

  4. Removing module(override = true) at CarPoolViewTest shows DefinitionOverrideException since module is already delcared.

  5. Break pointing RestClient at CarPoolRepositoryImpl it's possible to see that module was not override, actually the one being loaded is from production code at Injector class.

Expected behavior

  1. HttpClientStub should replace CoreHttpClientImpl in order to mock API calls

Koin project used and used version

koin-androidx-viewmodel 2.0.1 koin-test version 2.0.1

roliveiravictor avatar Oct 30 '19 13:10 roliveiravictor

@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.

roliveiravictor avatar Oct 30 '19 17:10 roliveiravictor

After further development, it was possible to conclude that the issue is not related to koin. Module's override feature is working properly.

roliveiravictor avatar Oct 31 '19 20:10 roliveiravictor

i have same problem then what is your solution?

tuinui avatar Aug 27 '20 18:08 tuinui

I have same problem here 😢

pedrofsn avatar May 10 '22 21:05 pedrofsn

same problem. single(override=true) {} was working well. Maybe you can define same function on koin test library?

slmyldz avatar Apr 14 '23 12:04 slmyldz