我已经宣布了这样的活动
class QuestionnaireActivity<T : ProfileModel> : AppCompatActivity()
我想写一个espresso测试,所以我写了像ActivityTestRule
@Rule @JvmField
val activityRule = object : ActivityTestRule<QuestionnaireActivity<ProfileModel.PersonalInfo>>(QuestionnaireActivity<ProfileModel.LifeStyleInfo>::class.java){
override fun getActivityIntent(): Intent = QuestionnaireActivity.getQuestionnaireIntent(InstrumentationRegistry.getTargetContext(), 3, ProfileModel.LifeStyleInfo())
}
但是编译器抱怨(它关于ActivityTestRule的参数)
only classes are allowed on the left hand side of a class literal
说明here仿制药不能与 class 一起使用。
如果删除泛型类型参数,则错误变为
Type inference failed.
Expected type mismatch: inferred type is Class<QuestionnaireActivity<*>> but Class<QuestionnaireActivity<ProfileModel.PersonalInfo>!>! was expected
我该怎么办?
感谢您的关注
答案 0 :(得分:0)
类似于here,解决方案涉及定义像这样的内联函数
inline fun <reified T: Activity> activityTestRuleWithIntent(intent: Intent) = object : ActivityTestRule<T>(T::class.java){
override fun getActivityIntent(): Intent = intent
}
然后规则变为:
@Rule @JvmField
val rule = activityTestRuleWithIntent<QuestionnaireActivity<ProfileModel.LifeStyleInfo>>(QuestionnaireActivity.getQuestionnaireIntent(InstrumentationRegistry.getTargetContext(), 3, ProfileModel.LifeStyleInfo()))