Me again :slightly_smiling_face: . I'm trying to f...
# koin
j
Me again 🙂 . I'm trying to follow these instructions to inject parameters in my objects. Since I'm not working on an Android app I tried to convert the instructions to a simple test case and came up with the following:
Copy code
import org.koin.dsl.module

class Presenter(val a : String, val b : String)

val presenterModule = module {
    single { params -> Presenter(a = params.get(), b = params.get()) }
}
and
Copy code
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.RegisterExtension
import org.koin.core.component.inject
import org.koin.core.parameter.parametersOf
import org.koin.test.KoinTest
import org.koin.test.junit5.KoinTestExtension

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class KoinExploration : KoinTest {

    @JvmField
    @RegisterExtension
    @Suppress("unused")
    val koinTestExtension = KoinTestExtension.create {
        modules(
            presenterModule
        )
    }

    @Test
    fun parameterTest() {
        val a = "bar"
        val b = "foo"
        val presenter : Presenter by inject { parametersOf(a, b) }
        println("Result: ${presenter.a} ${presenter.b}")
    }
}
I expected to get "Result: bar foo" but instead I got "Result: bar bar". Can anyone shed some light here?
z
The problem is that both parameters are the same type. In this case you should get them by index: https://insert-koin.io/docs/reference/koin-core/injection-parameters/#resolving-injected-parameters-in-order
Resolving injected parameters in order
Instead of using get() to resovle a parameter, if you have several parameters of the same type you can use the index as follow get(index) (also same as [ ] operator):
ł
Copy code
val presenterModule = module {
    single { (a: String, b: String) ->
        Presenter(a = get { parametersOf(a) }, b = get { parametersOf(b) })
    }
}
Replace with this, it should work
a
parameter injection is fixed in next 3.4.1 👍
you should obtain what you expect