I have an app that uses a ContentProvider to serve data instead of directly accessing the database. The application is working as expected but I have a problem with unit tests: how can I force the ContentProvider to use a test database rather than a 'live' one? If I wasn't using a ContentProvider I could pass a different database name to the Constructor of my SQLiteOpenHelper
sublass, but now the database is created with a call to onCreate() in my ContentProvider:
@Override
public boolean onCreate()
{
UKMPGDataProvider.init(getContext(), Constants.DATABASE_NAME);
return (UKMPGDataProvider.getWritableDatabase() == null) ? false : true;
}
As you can see, the database name is hardcoded.
Is there a way to pass a test name into the ContentProvider?
After a bit of digging, I found extending ProviderTestCase2 will automatically add a prefix to the database.