Espresso
02 / 02

Activities, RecyclerViews & Test Modules

Activities, RecyclerViews & Test Modules

ActivityScenario & FragmentScenario

@Test
fun test() {
    ActivityScenario.launch(MainActivity::class.java).use { scenario ->
        onView(withId(R.id.title)).check(matches(isDisplayed()))
    }
}
// modern, lifecycle-aware replacement for the older ActivityTestRule
// FragmentScenario.launchInContainer(MyFragment::class.java) tests a
// Fragment in relative isolation, without needing its full production host

RecyclerView Items (Espresso-Contrib)

onView(withId(R.id.recycler_view))
    .perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(2, click()))
// needed because off-screen recycled items aren't in the live view hierarchy
// for plain onView() matching to find

Navigation & Off-Screen Views

onView(withId(R.id.long_list_item)).perform(scrollTo(), click())
onView(isRoot()).perform(pressBack())

Espresso-Intents & Espresso-Web

Espresso-Intents stubs/verifies Android Intents (e.g. asserting a Share button launches the expected intent, without triggering a real external app). Espresso-Web bridges into WebView content, since a WebView's DOM isn't exposed as regular Android views onView() can match directly.

Where Tests Run

Espresso tests are instrumented tests (androidTest source set) running on a real device/emulator — slower than local JVM unit tests (test source set). Many teams run the fast unit suite on every commit and the fuller instrumented suite pre-merge or nightly.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free