gpt4 book ai didi

android - 访问 SQLite 数据库进行 Android 测试

转载 作者:行者123 更新时间:2023-11-28 20:54:28 24 4
gpt4 key购买 nike

我正在尝试为 Android 应用程序设置一些单元测试,但很难访问我的数据库。我在第一次使用时将数据库从 assets 目录复制到系统中。数据库设置如下:

private class DBOpenHelper extends SQLiteOpenHelper {
private Context context;

public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
this.context = context;

// Write a full path to the databases of your application
openDataBase();
}

// This piece of code will create a database if it’s not yet created
public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error");
throw new Error("Error copying database!");
}
} else {
Log.d("DB", "Delete");

Log.i(this.getClass().toString(), "Database already exists");
}
}

// Performing a database existence check
private boolean checkDataBase() {
SQLiteDatabase checkDb = null;
try {
String path = DB_PATH + DATABASE_NAME;
checkDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
Log.e(this.getClass().toString(), "Error while checking db");
}
// Android doesn’t like resource leaks, everything should
// be closed
if (checkDb != null) {
checkDb.close();
}
return checkDb != null;
}

// Method for copying the database
private void copyDataBase() throws IOException {
// Open a stream for reading from our ready-made database
// The stream source is located in the assets
InputStream externalDbStream = context.getAssets().open(
DATABASE_NAME);

// Path to the created empty database on your Android device
String outFileName = DB_PATH + DATABASE_NAME;

// Now create a stream for writing the database byte by byte
OutputStream localDbStream = new FileOutputStream(outFileName);

// Copying the database
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead);
}
// Don’t forget to close the streams
localDbStream.close();
externalDbStream.close();
}

public SQLiteDatabase openDataBase() throws SQLException {
String path = DB_PATH + DATABASE_NAME;

if (database == null) {
createDataBase();
database = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
}
Log.d("DB", "Open");

return database;
}

@Override
public synchronized void close() {
if (database != null) {
database.close();
}
Log.d("DB", "Close");

super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
Log.d("DB", "Create");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d("DB", "Update");
}

我使用单例访问它:

private static Database sInstance;

/**
* Open the database
*
* @param context The applications context
*/
public Database(Context context) {
DBOpenHelper openHelper = new DBOpenHelper(context);
database = openHelper.openDataBase();
}

public static synchronized Database getDatabase(
Context context) {
if (sInstance == null) {
sInstance = new Database(context);
}
return sInstance;
}

现在我尝试在我的 AndroidTestCase 中访问它

public class DatabaseTest extends AndroidTestCase {
Database database;

@Override
public void setUp() throws Exception {
super.setUp();
database = Database.getDatabase(getContext());

}
}

我也尝试过使用 ServiceTestCase 和 ApplicationTestCase 进行相同的设置,但我总是得到一个“空”上下文,从而导致在创建/打开我的数据库时出现 NPE。我现在正在尝试几个小时,而且似乎完全卡住了。我真的不知道我在这里错过了什么,任何人都可以告诉我一些事情吗?

我使用的数据库实际上是只读的,所以我不需要为单元测试安装一个单独的数据库。我'

最佳答案

您需要使用 RenamingDelegatingContext为了获得可用于打开数据库的上下文。

将 RenamingDelegatingContext 添加到您的 setUp() 函数中,如下所示:

public class DatabaseTest extends AndroidTestCase {
Database database;

@Override
public void setUp() throws Exception {
super.setUp();
RenamingDelegatingContext context
= new RenamingDelegatingContext(getContext(), "test_");

database = Database.getDatabase(context);
}
}

不要忘记在 tearDown() 函数中调用 database.close(); 关闭数据库。

引用:This project on GitHub .

关于android - 访问 SQLite 数据库进行 Android 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30971117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com