SQLiteOpenHelper 的 Android JUnit 测试 [英] Android JUnit test for SQLiteOpenHelper

查看:26
本文介绍了SQLiteOpenHelper 的 Android JUnit 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 junit 测试的新手.

I am new to junit testing.

谁能帮助我,如何测试我的 SQLiteOpenHelper 类.

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

表示我必须实现哪些类以及如何测试我的数据库和表.我正在使用 Eclipse IDE.

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

推荐答案

从 API 级别 24 开始,不推荐使用 RenamingDelegatingContext.另一个线程建议使用 RobolectricRuntimeEnvironment.application这篇 Medium 文章.

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

对于一个简单的 DatabaseHandler:

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

我创建了一个 AndroidTestCase:

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

这篇关于SQLiteOpenHelper 的 Android JUnit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆