0

I'm a little lost on how to properly test the database of my Android application. I have my Database Handler which extends SQLiteOpenHelper. Some solutions advise mocking this class, but how will that get me a separate instance of my database? I also see the suggestion to use an in-memory SQLite database but that seems like a lot of unnecessary juggling...

How should I be testing my database? Any help appreciated.

1 Answer 1

1

This is how I did for my database library:

public class TestDatabaseManager extends AndroidTestCase {

    HrSuiteDatabaseManager dbm;

    public void setUp() throws Exception {
        super.setUp();

        RenamingDelegatingContext rdcontext = new RenamingDelegatingContext(getContext(), "test_");
        dbm = new HrSuiteDatabaseManager(rdcontext);
        dbm.registerTableAccess(new EmployeeAccess());
    }

    public void testInsertion() {
        EmployeeAccess ea = dbm.getEmployeeAccess();
        ea.insert(createEmployee(0));

        List<Employee> emps = ea.getAll();
        assertEquals(1, emps.size());

        assertEmployee(emps.get(0), 0);
    }
}

The key here is the class RenamingDelegatingcontext that will create the database file with specified prefix.

I have HrSuiteDatabaseManager that is derived from SQLiteOpenHelper. I instantiate it using RenamingDelegatingcontext and use it in my test cases as usual.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.