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?
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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With