android - java.lang.RuntimeException:无法在单元测试中创建ViewModel类的实例

标签 android kotlin dagger dagger-hilt

我正在将Dagger-Hilt与Hilt Jetpack集成使用
我的生产依存关系

implementation "com.google.dagger:dagger:2.28"
kapt "com.google.dagger:dagger-compiler:2.28"

implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha01'

implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
我的测试依赖项
testImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
kaptTest "com.google.dagger:hilt-android-compiler:$hilt_version"

testImplementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha01'
kaptTest 'androidx.hilt:hilt-compiler:1.0.0-alpha01'
现在在我的 Activity 中,我使用的是jett扩展包中的by viewModels()扩展名
private val viewModel: SplashViewModel by viewModels()
在我的ViewModel中,我正在使用jetpack的@ViewModelInject
class SplashViewModel @ViewModelInject constructor(
    private val authenticationRepository: AuthenticationRepository
)
在生产代码中可以正常工作,但是在使用Robolectric从测试中启动 Activity 时,应用程序会因出现此异常而崩溃
java.lang.RuntimeException: Cannot create an instance of class com.example.SplashViewModel
这是我的考试课
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.O_MR1], application = TestApplication::class)
@HiltAndroidTest
class SplashActivityTest {

    private val authenticationRepository: AuthenticationRepository = mockk(relaxed = true)

    @get:Rule
    val rule = HiltAndroidRule(this)

    @get:Rule
    var activityRule = IntentsTestRule(SplashActivity::class.java)
}
这是我的TestApplication
class TestApplication : MultiDexApplication(), GeneratedComponentManager<Any>,
    TestApplicationComponentManagerHolder {

    private var componentManager: TestApplicationComponentManager? = null

    override fun onCreate() {
        super.onCreate()
        JodaTimeAndroid.init(this)
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        componentManager = TestApplicationComponentManager(this)
    }

    override fun componentManager(): Any? {
        return componentManager
    }

    override fun generatedComponent(): Any {
        return componentManager!!.generatedComponent()
    }
}

最佳答案

问题是从测试用例启动 Activity 时,它是“正常” Activity ,而不是用@AndroidEntryPoint注释的Hilt Activity 。
为了解决此问题,您必须(1)创建要启动的@AndroidEntryPoint Activity ,(2)为此创建调试方案。
这是您需要的所有内容的简单教程:https://www.youtube.com/watch?v=k4zG93ogWFY&t
https://www.youtube.com/watch?v=B-dJTFeOAqw&t

关于android - java.lang.RuntimeException:无法在单元测试中创建ViewModel类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62818294/

相关文章:

android - 当我弹出 BackStack 时,自定义 fragment 过渡动画无法正确播放

kotlin - 多卡 : include code samples into package documentation

java - Dagger 2 如何解决构造函数依赖

android - Dagger 错误 - 模块上没有代码生成

android - Dagger ObjectGraph plus() 包含根模块中的模块的模块不断返回 无无参数构造函数

android - 如何在 Fragment 中更改 Activity AppbarLayout

Android Studio 3.1.4 问题

android - 如何将项目位置设置为bottomcenter launchbackground.xml

android - 如何使用协同程序在 Room db 中获取插入查询的 ID

java - 不会在 SD 卡中创建文本文件