【问题标题】:How to get context in Robolectric Unit test with SQLite in Android如何在 Android 中使用 SQLite 在 Robolectric 单元测试中获取上下文
【发布时间】:2020-09-24 09:07:36
【问题描述】:

我想在 Android 中使用 Robolelectric 为 SQLiteOpenHelper 做一些单元测试。但是,我无法创建数据库,因为我无法获取上下文。我尝试了这里提到的 4 个选项How can we access context of an application in Robolectric?,但没有一个有效。在这里您可以看到 testclass 的代码,在“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"


}

【问题讨论】:

    标签: java android sqlite junit4 robolectric


    【解决方案1】:

    我很确定您无法使用 robolectric 测试 SQLite。例如,我的房间测试仅在 AndroidTest/Instrumented 中运行。如果你愿意改用 Room 而不是直接使用 SQLite,它的测试是有据可查的。

    如果您想了解更多关于房间的信息,请联系this code lab

    如果您想了解有关其测试的更多信息,可以查看here

    【讨论】:

    • 感谢 Henrique 的回答。基本上我发现了几个网页声称你可以用 robolectric 测试 SQLite,比如 medium.com/mobile-app-development-publication/…github.com/robolectric/robolectric/blob/master/robolectric/src/…
    • 但无论如何,我总觉得用单元测试为我的应用程序测试数据库根本不值得。我可以通过查看数据库的更改来测试数据库。这样我就不必根据我的观点实施所有那些不值得花时间的测试
    • 而且我不想使用 Room 而不是 SQLiteOpenHelper,因为我只是设法了解 SQLiteOpenHelper,并且不想一直学习新东西。
    • 嘿,我完全理解,一直学习新事物可能非常耗时。但老实说,我建议您使用您的 todolist 来学习它。它真的很容易、干净且易于测试。如果你以某种方式使用匕首,我有这个 repo 可以帮助你。 github.com/chenriquevz/DesafioOrama
    • 顺便问一下,你能用崩溃/错误的日志更新你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多