This library adds support for Koin to the test-utils-base
module
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")
}
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)
}
}
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) }
BaseKoinFlowTest
or KoinFlowTest
can be used to test Flows in a cross thread manner while maintaining access to Koin.