简体   繁体   中英

Koin Android Test

I have a problem with Koin & "androidTest". Because androidTest starts the Application i don't need to start Koin by myself in the test.

Now i need to inject a mock service. The problem is, that i inject inside of a method with get() inside of a singleton class and this is not working via constructor injection because the injected object can have different implementations.

My idea was to declare what i need this way:

declare {
        factory<Webservice>(override = true) { mockWebservice }
    }

But this will be applied on all tests. That's why an other test, which checks if the correct class was injected failed.

I also tried to use stopKoin(), startKoin(listOf(appModule)) in the @After method, but with this the dependency injection doesn't work anymore in later tests.

Is there a way to declare the mock only for one test?

Here is how I do it in my Android Tests:

In a parent test class, I use these methods for setup and teardown:

@Before fun startKoinForTest() {
    if (GlobalContext.getOrNull() == null) {
        startKoin {
            androidLogger()
            androidContext(application)
            modules(appComponent)
        }
    }
}

@After fun stopKoinAfterTest() = stopKoin()

My appcomponent contains all modules needed for the dependency tree.

Then, when I want to mock a dependency for a specific test, I use something like this:

declareMock<TripApi> { given(this.fetch(any())).willReturn(TestData.usaTrip) }

You will need to add a new mock declaration for each test if you wish to swap a dependency with a mock.

To declare mock only for one test you can use loadKoinModules()

You can't call the startKoin() function more than once. But you can use directly the loadKoinModules() functions.

So this way your definition will override default one

loadKoinModules(module {
    factory<Webservice>(override = true) { mockWebservice }
})

Also, don't forget to implement KoinTest interface in you test class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM