我正在使用带有刀柄喷气背包集成的匕首刀柄
我的生产依赖项
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'
现在在我的活动中,我正在使用by viewModels()
扩展表单喷气背包
private val viewModel: SplashViewModel by viewModels()
在我的 ViewModel 中,我使用@ViewModelInject
的是 jetpack
class SplashViewModel @ViewModelInject constructor(
private val authenticationRepository: AuthenticationRepository
)
它在生产代码中运行良好,但是当使用 Robolectric 从测试中启动活动时,应用程序将因此异常而崩溃
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()
}
}