Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntentsRule deprecated Espresso

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?

like image 805
StuartDTO Avatar asked May 12 '26 10:05

StuartDTO


1 Answers

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()
}
like image 81
Martin Zeitler Avatar answered May 14 '26 11:05

Martin Zeitler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!