The following is the first few sections of a chapter from Android's Architecture Components, plus headings for the remaining major sections, to give you an idea about the content of the chapter.

Testing Room

Once you have a RoomDatabase and its associated DAO(s) and entities set up, you should start testing it.

The good news is that testing Room is not dramatically different than is testing anything else in Android. Room has a few characteristics that make it a bit easier than some things to test, as it turns out.

Writing Instrumentation Tests

On the whole, writing instrumentation tests for Room — where the tests run on an Android device or emulator — is unremarkable. You get an instance of your RoomDatabase subclass and exercise it from there.

So, for example, here is an instrumentation test case class to exercise the TripDatabase from the preceding chapter:


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class TripTests {
  TripDatabase db;
  TripStore store;

  public void setUp() {
    db=TripDatabase.create(InstrumentationRegistry.getTargetContext(), true);

  public void tearDown() {

  public void basics() {
    assertEquals(0, store.selectAll().size());

    final Trip first=new Trip("Foo", 2880);


    assertTrip(store, first);

    final Trip updated=new Trip(, "Foo!!!", 1440);

    assertTrip(store, updated);

    assertEquals(0, store.selectAll().size());

  private void assertTrip(TripStore store, Trip trip) {
    List<Trip> results=store.selectAll();

    assertEquals(1, results.size());
    assertTrue(areIdentical(trip, results.get(0)));

    Trip result=store.findById(;

    assertTrue(areIdentical(trip, result));

  private boolean areIdentical(Trip one, Trip two) {
    return( &&
      one.title.equals(two.title) &&

Here, we:

Using In-Memory Databases

When testing a database, though, one of the challenges is in making those tests “hermetic”, or self-contained. One test method should not depend upon another test method, and one test method should not affect the results of another test method accidentally. This means that we want to start with a known starting point before each test, and we have to consider how to do that.

One approach — the one taken in the above TripTests class — is to use an in-memory database. The static create() method on TripDatabase, if you pass true for the second parameter, creates a TripDatabase backed by memory, not disk.

There are two key advantages for using an in-memory database for instrumentation testing:

  1. It is intrinsically self-contained. Once the TripDatabase is closed, its memory is released, and if separate tests use separate TripDatabase instances, one will not affect the other.
  2. Reading and writing to and from memory is much faster than is reading and writing to and from disk, so the tests run much faster.

On the other hand, this means that the instrumentation tests are useless for performance testing, as (presumably) your production app will actually store its database on disk. You could use Gradle command-line switches, custom build types and buildConfigField, or other means to decide when tests are run whether they should use memory or disk.

Importing Starter Data

The one downside to having an empty starter database, such as a fresh in-memory database, is that you have no data. Eventually, you need some data to test.

That could come from test code, such as what TripTests does. In many cases, this is a necessary part of testing, to confirm that all of your DAO methods work as expected.

Alternatives include:

Writing Unit Tests via Mocks

The preview of this section was abducted by space aliens.