Search code examples
kotlinkoin

How can I check Koin modules while using injection parameters?


I would like to check my configuration by using the checkModules() method provided by koin-test as explained here.

However, I am using injection parameters and my test fails with an exception:

org.koin.core.error.NoParameterFoundException: Can't get parameter value #0 from org.koin.core.parameter.DefinitionParameters@3804648a

Here is a simple test to demonstrate the issue:

import org.junit.Test
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.check.checkModules

class TestCase : KoinTest {
    @Test
    fun checkModules() {
        koinApplication {
            modules(
                module { factory { (msg: String) -> Message(msg) } }
            )
        }.checkModules()
    }

    data class Message(val message: String)
}

Is there a way to make this work? How could I provide the missing parameter?


Solution

  • You need to pass this parameter to your test, like this:

    class TestCase : KoinTest {
        @Test
        fun checkModules() {
            koinApplication {
                modules(module { factory { (msg: String) -> Message(msg) } })
            }.checkModules {
                create<Message> { parametersOf("testMessage") }
            }
        }
    
    
        data class Message(val message: String)
    }
    

    Example from Koin repository: CheckModulesTest.kt#L156

    My issue with the same question: Issue