You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Activity testing on JUnit 5 is built on top of the Android ActivityScenario API. This guide is a great introduction to how that particular API can be used to drive an instrumentation test.
The android-test-core library provided by this repository gives developers an entry point to the ActivityScenario through an Extension Point. Its usage is analogous to how the old ActivityScenarioRule works in a JUnit 4 context.
Here is a minimal example:
classMyActivityTest {
@JvmField
@RegisterExtension
val scenarioExtension =ActivityScenarioExtension.launch<MyActivity>()
@Test
funmyTest() {
val scenario = scenarioExtension.scenario
// Do something with the scenario here...
}
}
Instead of being annotated with JUnit 4's @Rule, the extension is annotated with @RegisterExtension. For tests written in Kotlin, the @JvmField is also required, so that the runtime can process the extension properly.
Scenario as Parameter
When you use the ActivityScenarioExtension, you can also take advantage of JUnit 5's "parameter resolution" feature. A scenario can be passed to a test method directly as a parameter, saving you one line of code inside each test:
classMyActivityTest {
@JvmField
@RegisterExtension
val scenarioExtension =ActivityScenarioExtension.launch<MyActivity>()
@Test
funmyTest(scenario:ActivityScenario<MyActivity>) {
// Do something with the scenario here...
}
}