我有一个 dbManager 如下:
//TABLE_AUTH Column names
public static final String AUTH_CODE = "AuthCode";
public static final String SESSION_ID = "SessionId";
public static final String AUTH_TIME = "LastAuthorized";
public static final String ENCRYPT_KEY = "EncryptKey";
//TABLE_AUTH Create Statement
public static final String CREATE_AUTH_TABLE = "CREATE TABLE "
+ TABLE_AUTH + "("
+ AUTH_CODE + " TEXT, "
+ SESSION_ID + " TEXT, "
+ AUTH_TIME + " DATETIME, "
+ ENCRYPT_KEY
+" TEXT" +")";
public DBManager(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_AUTH_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_AUTH);
onCreate(db);
}
public static DBManager getInstance(Context ctx){
if(instance==null){
instance = new DBManager(ctx);
}
return instance;
}
我想用android测试用例测试这个数据库的写入和读取,所以我开发它如下:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.InstrumentationTestCase;
import android.util.Log;
import com.etlie.stockclient.DBManager.DBManager;
public class SQLiteTest extends InstrumentationTestCase {
DBManager databaseManager;
SQLiteDatabase dbWrite;
SQLiteDatabase dbRead;
ContentValues contentValues;
protected void setUp() throws Exception{
super.setUp();
databaseManager = DBManager.getInstance(getInstrumentation().getContext());
//TABLE AUTH TEST
dbWrite = databaseManager.getWritableDatabase();
dbRead = databaseManager.getReadableDatabase();
contentValues = new ContentValues();
}
public void testReadWriteAuthTable() throws Exception{
contentValues.put(databaseManager.AUTH_CODE, "auth code");
contentValues.put(databaseManager.SESSION_ID, "session id");
contentValues.put(databaseManager.AUTH_TIME, System.currentTimeMillis());
contentValues.put(databaseManager.ENCRYPT_KEY, "encrypt key");
dbWrite.insert(databaseManager.TABLE_AUTH, null, contentValues);
String selectQuery = "SELECT * FROM " + databaseManager.TABLE_AUTH;
Log.i(databaseManager.LOG_TAG, selectQuery);
Cursor c = dbRead.rawQuery(selectQuery, null);
if(c!=null)
c.moveToFirst();
String authCode = c.getString(c.getColumnIndex(databaseManager.AUTH_CODE));
String sessionId = c.getString(c.getColumnIndex(databaseManager.SESSION_ID));
String encryptKey = c.getString(c.getColumnIndex(databaseManager.ENCRYPT_KEY));
Log.i("AUTH CODE: ", authCode);
Log.i("SESSION ID: ", sessionId);
Log.i("ENCRYPT KEY: ", encryptKey);
assertEquals(authCode, "auth code");
assertEquals(sessionId, "session id");
assertEquals(encryptKey, "encrypt key");
}
}
但是,当我运行测试套件时,它在我尝试初始化 dbWrite 的地方崩溃了
dbWrite = databaseManager.getWritableDatabase();
我收到错误“错误代码 = 14,无法打开数据库”。我曾认为我的数据库创建代码等有问题。
然后我通过创建一个测试活动以不同的方式进行测试:
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DBManager dbManager = DBManager.getInstance(getApplicationContext());
SQLiteDatabase sqLiteDatabase = dbManager.getWritableDatabase();
SQLiteDatabase sqLiteDatabase1 = dbManager.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(dbManager.AUTH_CODE, "auth code");
contentValues.put(dbManager.SESSION_ID, "session id");
contentValues.put(dbManager.AUTH_TIME, System.currentTimeMillis());
contentValues.put(dbManager.ENCRYPT_KEY, "encrypt key");
sqLiteDatabase.insert(dbManager.TABLE_AUTH, null, contentValues);
String selectQuery = "SELECT * FROM " + dbManager.TABLE_AUTH;
Cursor c = sqLiteDatabase1.rawQuery(selectQuery, null);
if(c!=null)
c.moveToFirst();
String authCode = c.getString(c.getColumnIndex(dbManager.AUTH_CODE));
String sessionId = c.getString(c.getColumnIndex(dbManager.SESSION_ID));
String encryptKey = c.getString(c.getColumnIndex(dbManager.ENCRYPT_KEY));
Log.i("AUTH CODE: ", authCode);
Log.i("SESSION ID: ", sessionId);
Log.i("ENCRYPT KEY: ", encryptKey);
}
});
}
}
这记录了数据库中的每个条目没有问题,所以我知道数据已成功写入和读取。
任何人都可以看到测试套件代码可能有什么问题吗?我想为这个项目编写测试用例,如果我想不通,我就不能这样做......