Skip to content

Latest commit

 

History

History

test-utils-koin

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Test Utils Koin

This library adds support for Koin to the test-utils-base module

Installing

This library is available on Maven Central. You can import Kaluga Test Utils Koin as follows:

repositories {
    // ...
    mavenCentral()
}
// ...
dependencies {
    // ...
    implementation("com.splendo.kaluga:test-utils-koin:$kalugaVersion")
}

Testing with Koin on the UI thread

The BaseKoinUIThreadTest or KoinUIThreadTest class can be extended to configure Koin from the UI thread before running a test:

class MyKoinUIThreadTest : BaseKoinUIThreadTest<MyKoinUIThreadTest.Configuration, MyKoinUIThreadTest.MyKoinTestContext>() {

    data class Configuration(val initialValue: String)
    
    class MyKoinTestContext(configuration: Configuration, coroutineScope: CoroutineScope) : KoinUIThreadTest.KoinTestContext(
        // constructor of takes app declarations and modules as parameters
        module {
            single { configuration.initialValue }
        }
    ) {
        // test injection into context
        val k: String by inject()
    }

    override val createTestContextWithConfiguration = { (configuration, scope) -> MyKoinTestContext(configuration, scope) }

    @Test
    fun testKoinUIThreadViewModelTest() = testOnUIThread(Configuration("K")) {
        assertEquals("K", k)
    }
}

Testing ViewModels

In case both Koin and a ViewModel are used, there is KoinUIThreadViewModelTest. If Koin provides the ViewModel you can inject it inside the TestContext:

override val viewModel: KoinViewModel by inject()

if not you can use lazy initialization:

override val viewModel by lazy { MyViewModel(myArgs) }

Testing flows

BaseKoinFlowTest or KoinFlowTest can be used to test Flows in a cross thread manner while maintaining access to Koin.