Issue
I've been using a BaseTest class for Espresso something like this :
abstract class BaseTest<T : Activity> {
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
abstract fun getTestActivity(): IntentsTestRule<T>
abstract fun startIntentActivity()
@Rule
@JvmField
var activityTestRule = this.getTestActivity()
fun launchActivity(intent: Intent?) {
getTestActivity().launchActivity(intent)
}
@Before
fun setUp() {
startIntentActivity()
}
@After
fun tearDown() {
activityTestRule.finishActivity()
}
}
But now I'm seeing that abstract fun getTestActivity(): IntentsTestRule<T> is deprecated and we should use ActivityScenario and ActivityScenarioRule how should I modify that class to use the new clases?
Solution
Generally one can provide the Intent either with Espresso - or an ActivityScenarioRule:
ActivityScenarioRule(Intent startActivityIntent)
Constructs ActivityScenarioRule with a given intent.
In Kotlin:
lateinit var scenario: ActivityScenario<SomeActivity>
val intent = Intent(ApplicationProvider.getApplicationContext(), SomeActivity::class.java)
@get:Rule
val activityRule = activityScenarioRule<SomeActivity>(intent)
@Test
fun someTest() {
scenario = rule.scenario
scenario.onActivity { activity ->
...
}
}
@After
fun cleanup() {
scenario.close()
}
Answered By - Martin Zeitler
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.