Koin: Qualifier not working on koin test

Created on 18 Sep 2019  路  2Comments  路  Source: InsertKoinIO/koin

Describe the bug

After naming a qualifier to set it as parameter to an injection while writing a white-box test, NoBeanDefFoundException is being triggered.

org.koin.core.error.NoBeanDefFoundException: No definition found for qualifier='null' & class='class com.imagebucket.main.repository.GalleryRepositoryImpl (Kotlin reflection is not available)'

To Reproduce

Test

class GalleryRepositoryTest: AutoCloseKoinTest() {

    private val module = module {
            single<GalleryRepository>(named("repository")) { GalleryRepositoryImpl() }
            viewModel { GalleryViewModel(get()) }
    }

    private val repository: GalleryRepositoryImpl by inject()
    private val vm: GalleryViewModel by inject(named("repository"))

    @Before
    fun setup() {
        startKoin { loadKoinModules(module) }
        declareMock<GalleryRepositoryImpl>()
    }

    @Test
    fun uploadImage_withEmptyUri_shouldDoNothing() {
        vm.delete("")
        verify(repository).delete("")
    }
}

VM


class GalleryViewModel(private val repository: GalleryRepository) : ViewModel() {

    fun delete(name: String) {
        repository.delete(name)
    }
}

Expected behavior

Green test by verifying that repository.delete method is being called.

Koin project used and used version (please complete the following information):

koin-test version 2.0.1
koin version 2.0.1

koin-test question check

All 2 comments

Hello @roliveiravictor,

there is perhaps a mistake. From:

private val module = module {
            single<GalleryRepository>(named("repository")) { GalleryRepositoryImpl() }
            viewModel { GalleryViewModel(get()) }
    }

    private val repository: GalleryRepositoryImpl by inject()
    private val vm: GalleryViewModel by inject(named("repository"))

I think you have multiple fixes:

private val module = module {
            single<GalleryRepository>(named("repository")) { GalleryRepositoryImpl() }
            viewModel { GalleryViewModel(get(named("repository"))) }
    }
private val repository: GalleryRepository by inject(named("repository"))
    private val vm: GalleryViewModel by inject()

Thank you @arnaudgiuliani

Was this page helpful?
0 / 5 - 0 ratings