Android的JUnit测试SQLiteOpenHelper测试、Android、JUnit、SQLiteOpenHelper

2023-09-04 09:22:38 作者:对伱ヽ何止一句喜欢

我是新来的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.

推荐答案

对于一个简单的数据库处理器:

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 wich is not connected to the standard database of the App
    }
}
 
精彩推荐