Kartihkraj Duraisamy Kartihkraj Duraisamy - 1 year ago 101
Android Question

Android JUnit test for SQLiteOpenHelper

I am new to junit testing.

Can anyone help me , how to test my


Means what classes I have to implement and how to test my db and tables.
I am using Eclipse IDE.

Answer Source

For a simple DatabaseHandler:

public class MyDatabase extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "database.db";
    private static final int DATABASE_VERSION = 1;

    public MyDatabase(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    public void onCreate(SQLiteDatabase db){
        // some code

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // some code

I created an AndroidTestCase:

public class DatabaseTest extends AndroidTestCase {
    private MyDatabase db;

    public void setUp() throws Exception {
        RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), "test_");
        db = new MyDatabase(context);

    public void tearDown() throws Exception {

    //According to Zainodis annotation only for legacy and not valid with gradle>1.1:
    public void testAddEntry(){
        // Here i have my new database wich is not connected to the standard database of the App
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download