简体   繁体   中英

Koin: NoBeanDefFoundException, Check your module definitions

The koin test results as follows:

org.koin.core.error.NoBeanDefFoundException: No definition found for '<class_name>' has been found. Check your module definitions.

the class EmailValidatorUtilImpl is well implemented,

import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.dsl.module
import org.koin.test.KoinTest
import org.koin.test.inject

class EmailValidatorUtilImpl : EmailValidatorUtil {

    private val pattern = Pattern.compile(EMAIL_PATTERN)
    private var matcher: Matcher? = null

    override fun validateEmail(email: String): Boolean {
        matcher = pattern.matcher(email)
        return matcher!!.matches()
    }

    companion object {
        private val EMAIL_PATTERN = "^[a-zA-Z0-9#_~!$&'()*+,;=:.\"(),:;<>@\\[\\]\\\\]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*$"
    }
}

and also, injected in KoinTest as below:


class EmailValidatorUtilImplTest : KoinTest, KoinComponent {

    private val validatorUtilImpl: EmailValidatorUtilImpl by inject()

    @Before
    fun setUp() {
        startKoin { module { single { EmailValidatorUtilImpl } } }
    }

    @Test
    fun `is valid email returns true`() {
        val isEmailValid = validatorUtilImpl.validateEmail("someone@somewhere.com")
        Assert.assertTrue(isEmailValid)
    }

    @Test
    fun `is invalid email returns false`() {
        val isEmailValid = validatorUtilImpl.validateEmail("invalid_email")
        Assert.assertFalse(isEmailValid)
    }

    @After
    fun tearDown() {
        stopKoin()
    }
}

furthermore, the implementation class is been well injected as


var loginUtilsModule = module {
    single { EmailValidatorUtilImpl() }
}

in Application class:

startKoin {
            androidLogger(Level.DEBUG)
            androidContext(this@SampleApplication)
            modules(listOf(
                    loginUtilsModule
            ))
        }

dependencies (app/build.gradle):


    // di
    implementation 'org.koin:koin-androidx-viewmodel:2.0.1'
    implementation 'org.koin:koin-androidx-scope:2.0.1'
    implementation 'org.koin:koin-android:2.0.1'

    // test
    testImplementation 'org.mockito:mockito-core:2.28.2'
    testImplementation 'org.koin:koin-test:2.0.1'
    testImplementation 'junit:junit:4.12'

    // android test
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'org.koin:koin-test:2.0.1'

I found the issue and the mistake was the module instead of modules (or.koin.core.KoinApplication)

    @Before
    fun setUp() {
        startKoin { module { single { EmailValidatorUtilImpl } } }
    }

so, the solution and the correct version are:

    startKoin { modules(loginUtilsModule) }

I had the same problem, but in my case Koin was unable to resolve an implementation of interface. I had:

interface MessagesRepository {...}

class MessagesRepositoryImpl : MessagesRepository {...}

class GetMessagesUseCase(private val messagesRepository: MessagesRepository) {...}

And in Koin module I wrote:

single { MessagesRepositoryImpl() }
single { GetMessagesUseCase(get()) }

So Koin couldn't find an instance of MessagesRepository to inject it into GetMessagesUseCase . Specifying singleton's type explicitly resolved the issue (but maybe there is a better solution):

single<MessagesRepository> { MessagesRepositoryImpl() }
single { GetMessagesUseCase(get()) }
 single { MessagesRepositoryImpl() as MessagesRepository }

Yamashiro Rion你可以这样做

I am getting similar error while refactoring my network module. Please find the network module below.

private const val TIME_OUT = 30L

val NetworkModule = module {

single { createService(get()) }

single { createRetrofit(get(), BuildConfig.BASE_URL) }

single { createOkHttpClient() }

single { MoshiConverterFactory.create() }

single { Moshi.Builder().build() }

}

fun createOkHttpClient(): OkHttpClient {
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
return OkHttpClient.Builder()
    .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
    .readTimeout(TIME_OUT, TimeUnit.SECONDS)
    .addInterceptor(httpLoggingInterceptor).build()
}

fun createRetrofit(okHttpClient: OkHttpClient, url: String): Retrofit {
return Retrofit.Builder()
    .baseUrl(url)
    .client(okHttpClient)
    .addConverterFactory(MoshiConverterFactory.create()).build()
}

fun createService(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}

fun createUsersRepository(apiService: ApiService): UserRepository {
return UserRepositoryImpl(apiService)
}

Issue occurred while moving the createUsersRepository to a separate repository module. Please provide some solution or let me know what I am doing wrong.

Please find the repository module below.

val RepositoryModule = module {

single { UserRepositoryImpl(get()) }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM