Hi All, I almost successfully migrated to koin com...
# koin
a
Hi All, I almost successfully migrated to koin compiler plugin. Almost because i have some issues regarding testing. So in the app itself all looks good the dependencies are generated, injected, all is fine. When it comes to testing, things I previously used, does not work anymore, specifically my custom test module. I have a feature modules with feature/ui, feature/data, feature/domain and feature/test. Each feature module has their KoinModule declared. The feature/test tests only feature/{ui,data,domain}. Previosly I was doing ``````
Copy code
object DeviceTestModule {
    val module =
        module {
            includes(DatabaseDi.module)
            includes(NoteDataDi.modules)
            includes(NoteDiModule.modules)
            includes(NoteUiDiModule.modules)
            includes(
                module {
                    factory { CoroutineScope(Dispatchers.Main.immediate + SupervisorJob()) }
                },
            )
        }
}
where module was an extension function generated on for the module during ksp run.
Now with the compiler plugin there is no such function generated.
And I don't understand yet how to bridge the generated on to the koin dsl. Since in the testing I would like to make a custom graph targeting only modules that I need with some substitutions for the test. And also can't find anything in the documentation about that.
A way around it is to declare the test graph from scratch using DSL, but having the ability to reuse the modules and to override them is so much nicer.
So find a way If I declare
Copy code
@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
Copy code
@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 not
A not so pretty way would be to declare two
Copy code
@Module
@ComponentScan("eu.caraus.kmp.notes.domain")
object NoteDiModule
^^ For tests/granular use and default module for prod
Copy code
@Module
@ComponentScan("eu.caraus.kmp.notes.domain")
@Configuration
object NoteDiConfig
Feels a bit cumbersome to me
Maybe there is a easier way to do this, and I am just going down a rabit hole
Okay, found a way to avoid two configurations, it doesn't work that way anyway So the key is to add configurations = [ "" ] to the test module like so
Copy code
@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.
So I have it like this if anyone is interested
Copy code
@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 dependencies
Copy code
class 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 rule
And I call it like so
Copy code
@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 {