neonDion
neonDion

Reputation: 2358

Android functional testing: clearing database between tests

I'm attempting to write functional tests for my Android application to test different login flows.

What I want to be able to do is start at the first activity and simulate a login with a certain user then start at the first activity again and simulate a login with a different user.

The issue I'm running into is that when I log a user in I write certain user information to a local database and when I attempt to log the second user in, the database already contains the first user's information and the tests fails.

What I think I want is a way to clear the database between test runs.

I found a way to delete the database using an ActivityTestRule, however this actually deletes the database.db file rather than just clearing the database. This causes the second test to fail as well because the database doesn't exist.

Is there a clean way to clear out the database between test runs?

Right now the only thing I can think of is to have one test class per login flow so that the application will actually have to restart its process between test runs which (along with deleting the db at the end of a test run) will allows the database to start up with a fresh db each time.

Upvotes: 0

Views: 1388

Answers (1)

neonDion
neonDion

Reputation: 2358

The issue seems to be that in my Application class onCreate() method I have Dagger creating an instance of my database. In my test code I delete that database file. So, prior to test1 running, Application's onCreate() gets run and the database gets built. When test1 finishes the database file gets deleted. When test2 starts the Application's onCreate() does not run because the app's process doesn't die between test methods. Therefore, when test2 attempts to access the db an error is thrown.

To solve this issue I added the following code to my tearDown() method:

    @After
        public void tearDown() throws Exception {
        deleteAllAppData();
        MyApplication application = ((MyApplication) activityActivityTestRule.getActivity().getApplicationContext());
        InstrumentationRegistry.getInstrumentation().callApplicationOnCreate(application);
    }

    private void deleteAllAppData() {
        activityActivityTestRule.getActivity().deleteDatabase(DbHelper.DATABASE_NAME);
        PreferenceManager
            .getDefaultSharedPreferences(activityActivityTestRule
                    .getActivity()
                    .getApplicationContext())
            .edit()
            .clear()
            .apply();
    }

Now what happens is test1 runs, then in the teardown method the database file gets deleted and then I get a handle to the a current Application class and then InstrumentationRegistry object is used to call onCreate() on the Application class. This simulates the process restarting and so my database gets created again and I have a fresh database ready for the next functional test.

Upvotes: 2

Related Questions