Sometimes you want to modify the db of the app, and then check the UI based on how it displays the items from the db. Here are some snippets of code to use in your test class to modify your db.

Firstly - it’s important your Activity doesn’t launch automatically at the start of each test. This is because we (well, I) want to modify the db in the test, and the Activity will get confused if you modify the db from under its feet.

We can stop the activity launching automatically by setting the last param in the constructor false as below:

public ActivityTestRule<MainActivity> mActivityTestRule = 
    new ActivityTestRule<>(MainActivity.class, false, false);

Next, here is an example of a function to clear the database.

private final Context mContext = InstrumentationRegistry.getTargetContext();
private final Class mDbHelperClass = YourDbHelper.class;
public void dropDatabase() throws Exception {
    SQLiteOpenHelper dbHelper = (SQLiteOpenHelper) mDbHelperClass
        .delete("YOUR_TABLE_NAME", null, null);

Similarly, to insert data:

private void insertSingleTask(ContentValues testValues) throws Exception {
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass

    SQLiteDatabase database = dbHelper.getWritableDatabase();

    long firstRowId = database.insert(

    assertNotEquals("Unable to insert into the database", -1, firstRowId);

If you want to use this in a test, you need to make your db changes first, and then call launchActivity(null) on your test rule:

public void fabLaunchesAddTaskActivity() throws Exception {
    // Given

    // Test your UI!