Home > Enterprise >  Launching Activity with extra attached to Intent in Instrumented test, running on espresso and JUnit
Launching Activity with extra attached to Intent in Instrumented test, running on espresso and JUnit

Time:05-08

I'd like to launch my ActivityScenarioExtension with an extra attached to the startActivityIntent. The extra-less scenario extension launching, which works just as intended, I carry out via

internal class TestMainActivity{
    @JvmField
    @RegisterExtension
    val scenarioExtension = ActivityScenarioExtension.launch<MainActivity>()

    @BeforeEach
    fun recreateActivity(scenario: ActivityScenario<MainActivity>){
        scenario.recreate()
    }
}

, however I'm not sure as to how to add in the required extra.

Not having found any real reference addressing how to achieve that, I simply tried

@JvmField
@RegisterExtension
val scenarioExtension = ActivityScenarioExtension.launch<MainActivity>(
    Intent().putParcelableArrayListExtra("CROP_URIS", ArrayList())
)

, which however led to

java.lang.RuntimeException: Unable to resolve activity for: Intent { (has extras) }
at androidx.test.core.app.InstrumentationActivityInvoker.startActivity(InstrumentationActivityInvoker.java:402)
at androidx.test.core.app.InstrumentationActivityInvoker.startActivity(InstrumentationActivityInvoker.java:437)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226)
at de.mannodermaus.junit5.ActivityScenarioExtension$Companion$launch$2.invoke(ActivityScenarioExtension.kt:130)
at de.mannodermaus.junit5.ActivityScenarioExtension$Companion$launch$2.invoke(ActivityScenarioExtension.kt:130)
at de.mannodermaus.junit5.ActivityScenarioExtension.beforeEach(ActivityScenarioExtension.kt:156)

The implementation propositions exclusively concerning JUnit 4 in turn, I've found in Espresso startActivity that depends on Intent, I didn't quite manage to transfer into the realm of JUnit 5 and its newly introduced Extension model.

Thanks in advance for whatever kind of help.

CodePudding user response:

Try this:

private val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java).apply {
    putParcelableArrayListExtra("CROP_URIS", ArrayList())
}

@Rule
@JvmField
val activity = ActivityScenarioRule<MainActivity>(intent)
  • Related