I am writing JUnit tests for my Android app. I have read through the Android developer resources (testing fundamentals, Spinner example test, etc.). Now I want to test my SQLiteOpenHelper subclass independently of the Activities which use it. My idea is to extend ActivityInstrumentationTestCase2<Activity>
. Is it okay to simply use Activity
as the generic parameter or do I need a subclass? Also, am I headed in the right direction here, or is there a better way to test my SQLiteOpenHelper subclass?

- 81,660
- 23
- 145
- 268
-
A normal `AndroidTestCase` should be sufficient. For testing purposes, using a `ContextWrapper` that mocks the `SQLiteDatabase` created by the database helper might be useful (which can be done by overriding `Context#openOrCreateDatabase(String, int, CursorFactory)`) – Jens Oct 20 '12 at 21:11
-
@Jens Thanks for the tip. I'll check it out. – Code-Apprentice Oct 21 '12 at 23:15
-
@Jens Can you provide some more details as an answer? In particular, I'm unclear about overriding `Context#openOrCreateDatabase()`. Do you mean creating a mock `Context` subclass which I pass to the `ContextWrapper`? – Code-Apprentice Oct 21 '12 at 23:56
-
Creating a subclass of ContextWrapper & override openOrCreateDatabase in that should work for you. – Jens Oct 22 '12 at 21:11
-
@Jens Just to clarify, why do you suggest subclassing `ContextWrapper` rather than `Context`? What is the difference? – Code-Apprentice Oct 22 '12 at 23:38
-
1Subclassing `Context` would mean you'd have to track down all methods used in `Context` by the `SQLiteOpenHelper` - and then call through to the wrapped `Context` returned by your test case - or return suitable mock values for each and every one - i.e. tedious and pointless busy-work. – Jens Oct 23 '12 at 07:30
3 Answers
I was looking for an answer to exactly this problem, and found this link as well as another interesting related question here:
The accepted answer by @Pietro shows some simple code, using a basic AndroidTestCase
, which will help directly to answer the question.
public class DatabaseTest extends AndroidTestCase {
private MyDatabase db;
public void setUp(){
RenamingDelegatingContext context
= new RenamingDelegatingContext(getContext(), "test_");
db = new MyDatabase(context);
}
public void testAddEntry(){
// Here I have my new database which is not connected to the standard database of the App
}
public void tearDown() throws Exception{
db.close();
super.tearDown();
}
}
I was happy at how simple it looks. In my case I'm new to Android testing, so even the simple stuff seems difficult at the moment.
But the interesting part which is key, is using the RenamingDelegatingContext
class as your "context" instead of just using a normal context. This seems to build on the comments made by @Jens.
This class wraps a given context and delegates most operations to that context. The useful part is that it performs database and file operations with a renamed database/file name
(see documentation online).
This allows your TEST code to use an actual different instance of the database to your PRODUCTION code - at least in my case this is going to be useful.
Here is another related post where the accepted answer says pretty much the same thing:
Some useful tips in there about using ContentProvider
instead (but that's a different issue for another day).

- 1
- 1

- 29,432
- 22
- 140
- 255
-
Thank you for the answer to my old question. This is something I am still struggling with. Since asking this question, I have seen the questions that you linked, but I was looking for tests for a ContentProvider. Thank you for relating the same techniques back to testing a SQLiteOpenHelper. – Code-Apprentice Mar 06 '14 at 17:08
-
Sure thing, I find that collecting together different aspects of an issue is very helpful when I'm trying to learn a new approach to something. Also cleans up the site a bit to link the questions together. Hopefully some future readers will find this useful. – Richard Le Mesurier Mar 07 '14 at 06:23
-
@RichardLeMesurier I am actually stuck at the part where I need to create the tables. Because surely you'll have to trigger table creation in ur testcase as well? The Name "MyDatabase" is a tad deceiving, because new MyDatabase(context) creates a new SQliteHelper, and not the associated tables - or am I missing something? – AgentKnopf May 06 '15 at 21:32
following link talk about testing in android:
http://developer.android.com/tools/testing/testing_android.html
may be you have already seen it just posting it in case you have missed going through it.. it is a very good resource to learn about junit in android...

- 7,425
- 2
- 36
- 44
-
Thanks. I probably should have mentioned in my post that I have read through the Android testing resources. I am looking for information specific to testing a subclass of SQLiteOpenHelper. – Code-Apprentice Oct 20 '12 at 20:03
For testing with Android Studio,
You should use MockContext
instead of RenamingDelegatingContext
.
If you use RenamingDelegatingContext
, You would get context as null.
For AndroidTestCase, getContext()
would return null. And for InstrumentationTestCase, getInstrumentation().getContext()
would return null.
For further information, see this answer. https://stackoverflow.com/a/29063736/1020456