Is there an example somewhere how parameter injection for a class while at the same time an interfac...
j

Jerry Preissler

over 2 years ago
Is there an example somewhere how parameter injection for a class while at the same time an interface (that is implemented by the class o.c.) is bound to the bean. I have the class
class DokumentMemoryPersistenceAdapter
    (entries: List<Dokument>) : LoadDokumentPort, SaveDokumentPort
and the module
val persistenceKoinTestModule = module(createdAtStart = true) {

    single {
        (entries: List<Dokument>) ->
        DokumentMemoryPersistenceAdapter(entries = get { parametersOf(entries) })
    } withOptions {
        bind<LoadDokumentPort>()
        bind<SaveDokumentPort>()
    }
}
In my test I do the following
@BeforeEach
    fun beforeEach() {
        println("BeforeEach")
        val entries = emptyList<Dokument>()
        val persistenceAdapter: DokumentMemoryPersistenceAdapter by inject { parametersOf(entries) }
    }

    @Test
    fun loadDokumentOkTest() {
        val loadDokumentPort by inject<LoadDokumentPort>()
but as a result I get a message "could not create instance for DokumentMemoryPersistenceAdapter" with a root cause
Caused by: org.koin.core.error.NoParameterFoundException: Can't get injected parameter #0 from DefinitionParameters[] for type 'java.util.List'
	at app//org.koin.core.parameter.ParametersHolder.elementAt(ParametersHolder.kt:41)
	at app//org.codeshards.aktenordner.adapter.outgoing.persistence.PersistenceKoinTestModuleKt$persistenceKoinTestModule$1$1.invoke(PersistenceKoinTestModule.kt:22)
	at app//org.codeshards.aktenordner.adapter.outgoing.persistence.PersistenceKoinTestModuleKt$persistenceKoinTestModule$1$1.invoke(PersistenceKoinTestModule.kt:14)
	at app//org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:51)
	... 81 more
Lines 14 - 22 of my module happen to be the singleton declaration. I also tried to supply the parameter for the injected interfaces along the following sample
val saveDokumentPort: SaveDokumentPort by inject { parametersOf(entries) }
and got the same results. A similar test without a bind option works
class ListPresenter(val a: List<Dokument>)

val presenterModule = module {
    single {
            (a: List<Dokument>) -> ListPresenter(a = get { parametersOf(a) })
    }
}
...
    @Test
    fun listTest() {
        val a = emptyList<Dokument>()
        val listPresenter: ListPresenter by inject { parametersOf(a) }
so I guess the bind option is the problem. Any feedback would be appreciated.