IntentsRule 已弃用 Espresso

IntentsRule deprecated Espresso

我一直在使用 BaseTest class 来制作 Espresso,像这样:

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()
    }
}

但现在我发现 abstract fun getTestActivity(): IntentsTestRule<T> 已被弃用,我们应该使用 ActivityScenarioActivityScenarioRule 我应该如何修改 class 以使用新的类?

通常可以提供 Intent 浓缩咖啡 - 或 ActivityScenarioRule:

ActivityScenarioRule(Intent startActivityIntent)
Constructs ActivityScenarioRule with a given intent.

在科特林中:

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()
}