Issue
I have declared an activity like this
class QuestionnaireActivity<T : ProfileModel> : AppCompatActivity()
I want to write an espresso test so I'm writting ActivityTestRule like
@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())
}
but the compiler complains that(its about the argument of ActivityTestRule)
only classes are allowed on the left hand side of a class literal
It is stated here that generics can't be used with class.
If I remove the generic type parameter the error becomes
Type inference failed.
Expected type mismatch: inferred type is Class<QuestionnaireActivity<*>> but Class<QuestionnaireActivity<ProfileModel.PersonalInfo>!>! was expected
what should I do?
thanks for your attention
Solution
similar to here the solution involves defining an inline function like this
inline fun <reified T: Activity> activityTestRuleWithIntent(intent: Intent) = object : ActivityTestRule<T>(T::class.java){
override fun getActivityIntent(): Intent = intent
}
then the rule becomes:
@Rule @JvmField
val rule = activityTestRuleWithIntent<QuestionnaireActivity<ProfileModel.LifeStyleInfo>>(QuestionnaireActivity.getQuestionnaireIntent(InstrumentationRegistry.getTargetContext(), 3, ProfileModel.LifeStyleInfo()))
Answered By - saiedmomen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.