Testing a UI

To test a UI, we need to be able to set up that UI, perform some actions, and see what the results are. That will involve messing with our widgets.

For that widget manipulation, the Jetpack solution is Espresso. This provides a succinct (albeit strange) API for accessing widgets, checking their states, and performing actions on them (like clicks).

In this tutorial, we will write an Espresso test to test the RecyclerView created by RosterListFragment.

This is a continuation of the work we did in the previous tutorial. The book’s GitLab repository contains the results of the previous tutorial as well as the results of completing the work in this tutorial.

Step #1: Adding a New Test Class

If we are going to create tests for RosterListFragment, we should create a RosterListFragmentTest. And, since we do not have a ui.roster sub-package in androidTest, we will need to add that.

In the androidTest source set, right click over the com.commonsware.todo.repo package and choose “New” > “Package” from the context menu. Fill in com.commonsware.todo.ui.roster for the name, then click “OK” to make this sub-package.

Then, right-click over the new com.commonsware.todo.ui.roster package and choose “New” > “Kotlin File/Class” from the context menu. For the name, fill in RosterListFragmentTest and choose “Class” as the kind. Press Enter or Return to create the class, giving you:

package com.commonsware.todo.repo

class RosterListFragmentTest {
}

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.