Search code examples
androiddependency-injectionkoin

Can I unit test Koin Modules?


I'm using Koin as my DI framework in an Android project and I want to unit test my modules.

For example: if a class A depends on B and C, test that B and C are being included in the Koin graph and that I'm properly injecting them in the module.

Is it possible?


Solution

  • If you are using Junit, you can and it's very simple:

    Add the Koin Android dependency like below:

    // Add Maven Central to your repositories if needed
    repositories {
        mavenCentral()    
    }
    
    dependencies {
        
        // Koin for Tests
        testImplementation "io.insert-koin:koin-test-junit4:$koin_version"
    }
    

    Your test class:

    class CheckModulesTest : KoinTest {
    
        @Test
        fun checkAllModules() {
            appModule.verify()
        }
    }
    

    You can check the documentation on the Verifying Your App section