简体   繁体   English

如何在 Android 中使用 SQLite 在 Robolectric 单元测试中获取上下文

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

I would like to do some Unit test in Android for SQLiteOpenHelper using Robolelectric.我想在 Android 中使用 Robolelectric 为 SQLiteOpenHelper 做一些单元测试。 However, I can't create the database as I can't get the context.但是,我无法创建数据库,因为我无法获取上下文。 I tried out 4 options that are mentioned here How can we access context of an application in Robolectric?我尝试了这里提到的 4 个选项如何在 Robolectric 中访问应用程序的上下文? but none of them worked.但他们都没有工作。 Here you see the code of the testclass and in the "setUp" method you see the different options that I tried with the corresponding error message after //在这里,您可以看到测试类的代码,在“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);
    }
}

I also added the following lines in my build.gradle file:我还在 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'

Would you mind telling me what mistake I am making?你介意告诉我我犯了什么错误吗? I'd appreciate every comment.我会很感激每一条评论。

Update: As it seems that I might be missing some dependencies I uploaded the build.gradle file with the dependencies.更新:似乎我可能缺少一些依赖项,因此我上传了带有依赖项的 build.gradle 文件。 Would you mind telling me what the mistake is and why I can't get any context with Roblectric?您介意告诉我错误是什么以及为什么我无法获得 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"


}

I am pretty sure that you cannot test SQLite with robolectric.我很确定你不能用 robolectric 测试 SQLite。 My room tests, for instance, only run in AndroidTest/Instrumented.例如,我的房间测试仅在 AndroidTest/Instrumented 中运行。 If you are willing to change to Room instead of using SQLite directly, its testing is well documented.如果你愿意改用 Room 而不是直接使用 SQLite,它的测试是有据可查的。

If you want to learn more about room there is this code lab .如果您想了解有关 room 的更多信息,请访问此代码实验室

And if you wanna learn more about its testing you can check it here .如果你想了解更多关于它的测试,你可以在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM