Android JUnit test for SQLiteOpenHelper

前端 未结 4 1231
挽巷
挽巷 2020-12-08 00:52

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

相关标签:
4条回答
  • 2020-12-08 01:25

    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
       }
    }
    
    0 讨论(0)
  • 2020-12-08 01:27

    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

    0 讨论(0)
  • 2020-12-08 01:32

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

    The 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 which is not connected to the standard database of the App
        }
    }
    
    0 讨论(0)
  • 2020-12-08 01:37

    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.

    0 讨论(0)
提交回复
热议问题