gpt4 book ai didi

将 Koin 升级到 3.1.3 时,使用 sharedViewModel 的 Android 测试失败并出现 java.lang.ClassCastException androidx.lifecycle.SavedStateHandleController

转载 作者:行者123 更新时间:2023-12-04 23:49:39 35 4
gpt4 key购买 nike

我目前正在使用 Koin 版本 3.1.0 并想升级到最新版本(当前为 3.1.4),但是一旦我将 Koin 升级到 3.1.3 以上,我在运行测试时会遇到以下异常:

class java.lang.Object cannot be cast to class androidx.lifecycle.SavedStateHandleController (java.lang.Object is in module java.base of loader 'bootstrap'; androidx.lifecycle.SavedStateHandleController is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @5c2a3b10)
java.lang.ClassCastException: class java.lang.Object cannot be cast to class androidx.lifecycle.SavedStateHandleController (java.lang.Object is in module java.base of loader 'bootstrap'; androidx.lifecycle.SavedStateHandleController is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @5c2a3b10)
at androidx.lifecycle.SavedStateHandleController.attachHandleIfNeeded(SavedStateHandleController.java:98)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.onRequery(AbstractSavedStateViewModelFactory.java:103)
at androidx.lifecycle.StateViewModelFactory.onRequery(StateViewModelFactory.kt:37)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:160)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:139)
at org.koin.androidx.viewmodel.ViewModelResolverKt.resolveInstance(ViewModelResolver.kt:15)
at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:67)
at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExt.kt:53)
at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel$default(ScopeExt.kt:45)
at org.koin.androidx.viewmodel.ext.android.FragmentExtKt.getSharedViewModel(FragmentExt.kt:59)
at com.company.myapp.MyFragment$special$$inlined$sharedViewModel$default$2.invoke(FragmentExt.kt:61)
at com.company.myapp.MyFragment$special$$inlined$sharedViewModel$default$2.invoke(FragmentExt.kt:39)
at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
at com.company.myapp.MyFragment.getMyViewModel(MyFragment.kt:46)
at com.company.myapp.MyFragment.showButton(MyFragment.kt:196)
at com.company.myapp.MySecondFragment.onViewCreated(MySecondFragment.kt:49)
at com.company.myapp.MyFragment.onViewCreated(MyFragment.kt:56)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3019)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:551)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1758)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at org.robolectric.android.internal.RoboMonitoringInstrumentation.callActivityOnStart(RoboMonitoringInstrumentation.java:285)
at android.app.Activity.performStart(Activity.java:7157)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.robolectric.shadows._Activity_$$Reflector15.performStart(Unknown Source)
at org.robolectric.android.controller.ActivityController.lambda$start$4(ActivityController.java:129)
at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
at org.robolectric.android.controller.ComponentController.invokeWhilePaused(ComponentController.java:64)
at org.robolectric.android.controller.ActivityController.start(ActivityController.java:129)
at org.robolectric.android.internal.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:102)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:39)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226)
at com.company.myapp.test.ActivityTest.launchPresentationLayer(ActivityTest.kt:31)
at com.company.myapp.test.BaseTest.setup(BaseTest.kt:65)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at com.company.myapp.test.base.StopKoinRule$apply$1.evaluate(StopKoinRule.kt:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:570)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
我注意到它与 sharedViewModel 有关-extension 当我将某些事件更改为 by viewModel() 时导致测试将成功。但是,这种解决方法是 Not Acceptable ,因为我需要共享的 ViewModel。
有没有人可以解决这个问题?

最佳答案

将您的 koin 版本降级为 2.2.3应该修复它!
看到这个答案here

关于将 Koin 升级到 3.1.3 时,使用 sharedViewModel 的 Android 测试失败并出现 java.lang.ClassCastException androidx.lifecycle.SavedStateHandleController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70348679/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com