Alexandru Caraus
03/25/2026, 4:32 AMAlexandru Caraus
03/25/2026, 4:32 AMobject DeviceTestModule {
val module =
module {
includes(DatabaseDi.module)
includes(NoteDataDi.modules)
includes(NoteDiModule.modules)
includes(NoteUiDiModule.modules)
includes(
module {
factory { CoroutineScope(Dispatchers.Main.immediate + SupervisorJob()) }
},
)
}
}Alexandru Caraus
03/25/2026, 4:33 AMAlexandru Caraus
03/25/2026, 4:33 AMAlexandru Caraus
03/25/2026, 4:38 AMAlexandru Caraus
03/25/2026, 4:43 AMAlexandru Caraus
03/25/2026, 7:06 AM@Module
object TestSubstitutes {
@Factory
fun createViewModelCoroutineScope() =
CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
@Single
fun repo(): NoteRepository = NoteRepositoryInMem()
}
@KoinApplication(
modules = [
// DatabaseDi::class,
// NoteDiData::class,
NoteDiModule::class,
NoteUiDiModule::class,
TestSubstitutes::class
]
)
object TestNotesModule
and then in test I start startKoin<TestNotesModule> {}
but there is a but, NoteDiModule is a module and a configuration, and @KoinApplication TestNoteModule gets automatically all configurations I have,
since all my modules I like so
@Module
@ComponentScan("eu.caraus.kmp.notes.domain")
@Configuration
object NoteDiModule
It autowires the Configurations DatabaseDi and NoteDiData regardless if i indicated it in the modules or notAlexandru Caraus
03/25/2026, 7:07 AMAlexandru Caraus
03/25/2026, 7:08 AM@Module
@ComponentScan("eu.caraus.kmp.notes.domain")
object NoteDiModule
^^ For tests/granular use
and default module for prod
@Module
@ComponentScan("eu.caraus.kmp.notes.domain")
@Configuration
object NoteDiConfig
Feels a bit cumbersome to meAlexandru Caraus
03/25/2026, 7:09 AMAlexandru Caraus
03/25/2026, 7:34 AM@Module
object TestSubstitutes {
@Single
fun createViewModelCoroutineScope(): CoroutineScope =
CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
@Single
fun repo(): NoteRepository = NoteRepositoryInMem()
}
@KoinApplication(
configurations = [""],
modules = [
// DatabaseDi::class,
// NoteDiData::class,
NoteDiModule::class,
NoteUiDiModule::class,
TestSubstitutes::class
]
)
object TestNotesModule
This way it searches for named configurations, which I have none (otherwise it pulls all anyway and we get back to where started). And now it includes only the modules I specify, regardless of them being configurations as well.Alexandru Caraus
03/27/2026, 6:30 AMAlexandru Caraus
03/27/2026, 6:31 AM@Module
class AndroidTestSubstitutes {
@Factory
fun createViewModelCoroutineScope(): CoroutineScope =
CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
@Factory
fun appContext(): Context =
InstrumentationRegistry.getInstrumentation().targetContext
}
@KoinApplication(
configurations = [""],
modules = [
DatabaseDi::class,
NoteDiData::class,
NoteDiModule::class,
NoteUiDiModule::class,
AndroidTestSubstitutes::class
]
)
object IntegrationTestDi
^^ This is the test module dependenciesAlexandru Caraus
03/27/2026, 6:31 AMclass KoinAndroidTestRule(
private val start: (appDeclaration: KoinApplication.() -> Unit) -> Unit,
) : TestWatcher() {
override fun starting(description: Description) {
try {
start {}
} catch (_: KoinApplicationAlreadyStartedException) {
stopKoin()
start {}
}
}
override fun finished(description: Description) {
stopKoin()
}
}
^^ This the generic test ruleAlexandru Caraus
03/27/2026, 6:32 AMAlexandru Caraus
03/27/2026, 6:32 AM@RunWith(AndroidJUnit4::class)
class NotesNavigationTest : KoinTest {
@get:Rule
val composeTestRule = createComposeRule()
@get:Rule
val koinAndroidTestRule = KoinAndroidTestRule { startKoin<IntegrationTestDi>() }
@Before
fun setup() {
inject<AppDatabase>().value.clearAllTables()
inject<AppDatabase>().value.openHelper.readableDatabase
}
@Test
fun list_notes_then_create_note_then_list_notes() {
composeTestRule.setContent {