I need to write a test for an activity that either inserts or updates a database depending on what is sent to it as extras in the intent. To test this activity I first need to setup the database to aviod duplicate keys and other database-related problems.
The problem, to setup the database, i need a context and the only way I know how to get the context is to call getActivity() but then it will read the database before it is setup for the activity.
So, using a subclass of ActivityInstrumentationTestCase2, is it possible to setup the database before initializing the activity?
An psedudo-exampel of what I’d like to do.
public void testActivityWithDatabaseEmpty(){
//Setup database
SQLLiteDatabase myDb = getDatabase(); // how to do this in a test without first calling the activity?
emptyTestTable(myDb);
//Setup intent
Intent intent = getIntentWithNonExistingKey();
setActivityIntent(intent);
//Get activity
MyActivity activity = getActivity();
Button button = activity.findById(R.id.button);
assertAreEquals("", button.getText);
}
Thanks in advance
Roland
I do not know what exactly you need to do in the setup, but one approach could be to use the Application as context. I have personally gone a road where in my test, I use a special database name, not sure if that is applicable to your situation. I have described it in this blog post