簡體   English   中英

如何在 Android 中使用 SQLite 在 Robolectric 單元測試中獲取上下文

[英]How to get context in Robolectric Unit test with SQLite in Android

我想在 Android 中使用 Robolelectric 為 SQLiteOpenHelper 做一些單元測試。 但是,我無法創建數據庫,因為我無法獲取上下文。 我嘗試了這里提到的 4 個選項如何在 Robolectric 中訪問應用程序的上下文? 但他們都沒有工作。 在這里,您可以看到測試類的代碼,在“setUp”方法中,您可以看到我嘗試過的不同選項,並在 // 之后顯示了相應的錯誤消息

package com.example.td.barapp;

import android.content.Context;
import android.database.Cursor;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;


@RunWith(RobolectricTestRunner.class)


public class DataBaseHelperTest {

    private DataBaseHelper helperDB;


    @Before
    public void setUp() throws Exception {
        // Context context = ShadowApplication.getInstance().applicationContext; //Error: Cannot resolve symbol 'applicationContext'
        //Context context = Robolectric.application; // Error: Cannot resolve symbol application
        //Context context = ApplicationProvider.getApplicationContext(); // Error: Cannot resolve symbol 'ApplicationProvider'
        //Context context = RuntimeEnvironment.systemContext; // Error: Cannot resolve symbol 'systemContext'
//Context context = getClass().getClassLoader(); // Error: Required type: Context Provided: ClassLoader
        Context context = InstrumentationRegistry.getInstrumentation().getContext(); // Error: Cannot resolve symbol 'InstrumentationRegistry'
        helperDB = new DataBaseHelper(context);
    }

    @After
    public void tearDown() {
        helperDB.close();
    }

    @Test
    public void insertDataDB_TableRatings() {
        boolean insertRating = helperDB.insertDataDB_TableRatings("Orange Juice", "Any", "Any", 1,
                0,0,1,0,0 );

        Assert.assertTrue(insertRating);
    }

    @Test
    public void getDataDB_TableRating() {
        String drinkName = "Orange Juice";
        String drinkStrength = "Any";
        String drinkSize = "Any";

        boolean insertRating = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1,
                0,0,1,0,0 );


        Cursor res = helperDB.getDataDB_TableRating(drinkName, drinkStrength, drinkSize);
        Assert.assertFalse(res.getCount()==0);

        res.moveToFirst();
        Assert.assertEquals (res.getString(0), drinkName);
        Assert.assertEquals (res.getString(6), "1");

        drinkSize = "Large";

        boolean insertRating2 = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1,
                0,0,0,1,0 );

        res = helperDB.getDataDB_TableRating(drinkName, drinkStrength, drinkSize);
        Assert.assertFalse(res.getCount()==0);

        res.moveToFirst();
        Assert.assertEquals (res.getString(2), drinkName);
        Assert.assertEquals (res.getString(7), "1");


    }

    @Test
    public void updateDataDB_TableRatings() {
        String drinkName = "Orange Juice";
        String drinkStrength = "Any";
        String drinkSize = "Any";

        boolean insertRating = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1, 0,0,1,0,0 );
        boolean valueUpdated = helperDB.updateDataDB_TableRatings(drinkName, drinkStrength , drinkSize, 2, 0, 0, 0, 1, 0);

        Cursor res = helperDB.getDataDB_TableRating(drinkName, drinkStrength, drinkSize);
        Assert.assertFalse(res.getCount()==0);
        res.moveToFirst();
        Assert.assertEquals (res.getString(8), "1");
    }

    @Test
    public void deleteDataDB_TableRatings() {
        String drinkName = "Orange Juice";
        String drinkStrength = "Any";
        String drinkSize = "Large";

        boolean insertRating = helperDB.insertDataDB_TableRatings(drinkName, drinkStrength, drinkSize, 1, 0,0,1,0,0 );
        helperDB.deleteDataDB_TableRatings (drinkName, drinkStrength, drinkSize);
    }
}

我還在 build.gradle 文件中添加了以下幾行:

testImplementation 'junit:junit:4.13'
testImplementation 'org.robolectric:robolectric:3.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

你介意告訴我我犯了什么錯誤嗎? 我會很感激每一條評論。

更新:似乎我可能缺少一些依賴項,因此我上傳了帶有依賴項的 build.gradle 文件。 您介意告訴我錯誤是什么以及為什么我無法獲得 Roblectric 的任何上下文嗎?

apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.td.barapp"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        viewBinding {
            enabled = true
        }
        dataBinding {
            enabled=true
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    def nav_version = "2.3.0"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.2.0-alpha06'
    testImplementation 'junit:junit:4.13'
    testImplementation 'org.robolectric:robolectric:3.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'android.arch.core:core-testing:1.2.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation "androidx.recyclerview:recyclerview:1.1.0"
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"


}

我很確定你不能用 robolectric 測試 SQLite。 例如,我的房間測試僅在 AndroidTest/Instrumented 中運行。 如果你願意改用 Room 而不是直接使用 SQLite,它的測試是有據可查的。

如果您想了解有關 room 的更多信息,請訪問此代碼實驗室

如果你想了解更多關於它的測試,你可以在這里查看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM