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
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