Koin 依赖覆盖在测试中不起作用

Koin dependency override is not working in testing

我是测试新手,我将 Koin 改编为我的依赖注入。我的应用程序工作正常。它仍然具有登录功能。这是我的依赖 class

Modules.kt

val applicationModule = module (override = true) {
    single { NetworkService.getInstance().getService(APIService::class.java) }
    single { PreferenceManager.getDefaultSharedPreferences(androidContext()) }
}

val activityModule = module {

    scope(named<LoginActivity>()) {
        scoped { (activity: LoginActivity) ->
            Navigation
                .findNavController(activity, R.id.hostFragment)
        }
    }

    scope(named<MainNavigationActivity>()) {
        scoped { (activity: MainNavigationActivity) ->
            Navigation
                .findNavController(activity, R.id.hostFragment)
        }
    }
}

val viewModelModule = module {
    viewModel { LoginViewModel(loginRepository = get()) }
}

val repositoryModule = module (override = true) {
    single { LoginRepository() }
}

我正在尝试为 LoginRepository 中的登录功能编写一个简单的单元测试。我 MockWebServer 激发响应并获得结果。这是 LoginRepository

中的代码

登录存储库

class LoginRepository: KoinComponent {

    val network: APIService by inject()

    var loginMutableData = MutableLiveData<SingleLiveEvent<Resource<UserSession>>>()

    fun getLoginStatus(): LiveData<SingleLiveEvent<Resource<UserSession>>> {
        return loginMutableData
    }

    fun generalLogin(email: String, encryptedPassword: String){

        val login = network.login(email, encryptedPassword)

        login.enqueue(object : Callback<LoginResponse> {

            override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {

                if(response.isSuccessful){

                    if(response.body()?.status == 1){
                        val resource = Resource<UserSession>(true,"Success")
                        response.body().let {
                            if(it?.session != null){
                                resource.data = UserSession(it.session.userId!!,it.session.fullName!!)
                            }
                        }

                        loginMutableData.value = SingleLiveEvent(resource)

                    }else{

                        val resource = Resource<UserSession>(false,response.body()?.msg ?: "Login failed. Try again")
                        loginMutableData.value  = SingleLiveEvent(resource)
                    }

                }else{

                    loginMutableData.value = SingleLiveEvent(Resource(false, response.message()))
                }

            }

            override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
                loginMutableData.value = SingleLiveEvent(Resource(false, t.localizedMessage))
            }

        })

    }
}

所以我在下面写了测试 class 来测试 LoginRepository 中的这个 generalLogin(email: String, encryptedPassword: String) 函数。

LoginRepositoryTest

class LoginRepositoryTest : KoinTest {

    private val loginRepository: LoginRepository by inject()
    private val server by lazy { MockWebServer() }
    private lateinit var network: APIService

    @get:Rule
    val rule = InstantTaskExecutorRule()

    @Before
    fun setUp() {

        MockitoAnnotations.initMocks(this)

        startKoin {
            printLogger()
            modules(repositoryModule)
        }
    }

    @Test
    fun testLoginWithCorrectCredentials() {

        server.enqueue(
            MockResponse()
                .setResponseCode(200)
                .setBody("{\"msg\":\"success\",\"status\":1,\"session\":{\"userName\":\"Chathuran\",\"loggedin_user_email\":\"valid_email_address@gmail.com\"}}")
        )
        server.start()
        val testingUrl = server.url("account/userAuth/api_login/")

        network = NetworkService.getInstance().getService(APIService::class.java, testingUrl)
        loadKoinModules(module{network})

        val email = "valid_email_address@gmail.com"
        val password = "valid_password"

        loginRepository.generalLogin(email, password)

        loginRepository.getLoginStatus().observeForever {

            it.getContentIfNotHandled()?.also { resource ->
                Assert.assertEquals(email, resource.data?.email)
            }
        }
    }

    @After
    fun tearDown() {
        stopKoin()
        server.shutdown()
    }
}

所以在这个 class 中,我试图用我在测试调用中创建的实例覆盖 APIService 实例。这样我就可以告诉我的 Retrofit 实例使用 MockWebServer 提供的基础 URL。但是我从 Koin.

收到以下错误
org.koin.core.error.NoBeanDefFoundException: No definition found for 'com.findmyfare.mobile.app.network.APIService' has been found. Check your module definitions.

    at org.koin.core.scope.Scope.findDefinition(Scope.kt:170)
    at org.koin.core.scope.Scope.resolveInstance(Scope.kt:164)
    at org.koin.core.scope.Scope.get(Scope.kt:128)
    at com.findmyfare.mobile.app.repository.LoginRepository$$special$$inlined$inject.invoke(Scope.kt:327)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at com.findmyfare.mobile.app.repository.LoginRepository.getNetwork(LoginRepository.kt)
    at com.findmyfare.mobile.app.repository.LoginRepository.generalLogin(LoginRepository.kt:29)
    at com.findmyfare.mobile.app.repository.LoginRepositoryTest.testLoginWithCorrectCredentials(LoginRepositoryTest.kt:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

如果这是错误的,覆盖 APIService 实例的正确方法是什么?谢谢。

编辑:我的 build.gradle 依赖项

// Koin
    def koin_version = '2.0.1'
    implementation "org.koin:koin-androidx-scope:$koin_version"
    implementation "org.koin:koin-androidx-viewmodel:$koin_version"
    implementation "org.koin:koin-androidx-ext:$koin_version"
    testImplementation "org.koin:koin-test:$koin_version"

//Testing
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    testImplementation "org.mockito:mockito-core:2.21.0"
    testImplementation 'android.arch.core:core-testing:1.1.1'
    testImplementation 'com.squareup.okhttp3:mockwebserver:4.2.1'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

你实际上并没有用你的模块覆盖一个 bean

network = NetworkService.getInstance().getService(APIService::class.java, testingUrl)
loadKoinModules(module{network})

改为将网络 bean 声明为覆盖

network = NetworkService.getInstance().getService(APIService::class.java, testingUrl)
loadKoinModules(module { single(override=true) { network } })

此外,您不需要将 override 与主模块一起使用。

通过您的实施,您甚至不需要 override 以上内容。在测试之前,您没有使用 Koin 启动主模块。这可能会导致另一个问题,因此请确保拥有您需要的所有模块 运行.

startKoin {
    printLogger()
    modules(applicationModule, repositoryModule)
}