Android JUnit test for SQLiteOpenHelper

2019-01-16 14:43发布

问题:

I am new to junit testing.

Can anyone help me , how to test my SQLiteOpenHelper class.

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

回答1:

As of API Level 24, RenamingDelegatingContext is deprecated. Another thread suggests to use Robolectric's RuntimeEnvironment.application as described in this Medium article.

Old answer for reference:

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);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        // some code
    }

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

I created an AndroidTestCase:

public class DatabaseTest extends AndroidTestCase {
    private MyDatabase db;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), "test_");
        db = new MyDatabase(context);
    }

    @Override
    public void tearDown() throws Exception {
        db.close(); 
        super.tearDown();
    }

    //According to Zainodis annotation only for legacy and not valid with gradle>1.1:
    //@Test
    public void testAddEntry(){
        // Here i have my new database wich is not connected to the standard database of the App
    }
}


回答2:

You can write android database test in JUnit4 style as well. You just need to add following dependency in your database

androidTestCompile('com.android.support.test:runner:0.3'){
        exclude group: 'com.android.support', module: 'support-annotations'
    }

And mark you Test class as follows

@RunWith(AndroidJUnit4.class)
public class DatabaseTest {
   @Test
   public void myFirstTest(){
      //your test
   }
}


回答3:

RenamingDelegatingContext is now deprecated in v24. Use Robolectric to test. An example (in Kotlin) as below

@RunWith(RobolectricGradleTestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(LOLLIPOP), packageName = "com.elyeproj.simpledb")
class ExampleUnitTest {

    lateinit var dbHelper: DbHelper // This is your SQLOpenHelper class

    @Before
    fun setup() {
        dbHelper = DbHelper(RuntimeEnvironment.application) // Your new context from Robolectric
        dbHelper.clearDbAndRecreate() // A function just to clear and recreate DB
    }

    @Test
    @Throws(Exception::class)
    fun testDbInsertion() {

        // Given
        val testStr1 = "testing"
        val testStr2 = "testing"

        // When
        dbHelper.insertText(testStr1)
        dbHelper.insertText(testStr2)

        // Then
        assertEquals(dbHelper.getAllText(), "$testStr1-$testStr2-")
    }

    @After
    fun tearDown() {
        dbHelper.clearDb() // A function just to clear the DB
    }
}

You could get the entire project source from https://github.com/elye/demo_simpledb_test

You could get elaboration from https://medium.com/@elye.project/android-sqlite-database-unit-testing-is-easy-a09994701162#.s44tity8x



回答4:

this question is very abstract. I recommend that you read the Android Testing Guide. It has some simple examples of testing Android Apps.

Android Testing Support Library

Android Testing Training

Without knowing your code, I suppose that your SQLiteOpenHelper is used by an Activity or Service. I think that this Activities/Services is that you must to test.

A good start should be ActivityInstrumentationTestCase2 for an Activity or ServiceTestCase for a Service.

I hope it help.