0

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

yole
  • 92,896
  • 20
  • 260
  • 197
saiedmomen
  • 1,401
  • 16
  • 33

1 Answers1

0

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()))
saiedmomen
  • 1,401
  • 16
  • 33