【发布时间】:2020-07-09 15:23:13
【问题描述】:
我正在使用匕首与刀柄喷气背包集成
我的生产依赖项
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 中,我使用的是 by viewModels() 扩展表单 jetpack
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()
}
}
【问题讨论】:
标签: android kotlin dagger dagger-hilt