android testcase junit回滚
当我运行我的测试时,所有的东西都保存在我的sqllite数据库中,所以我想在我的Junit测试完成后回滚我的Android数据库,这可能吗android testcase junit回滚,android,junit,android-sqlite,android-testing,Android,Junit,Android Sqlite,Android Testing,当我运行我的测试时,所有的东西都保存在我的sqllite数据库中,所以我想在我的Junit测试完成后回滚我的Android数据库,这可能吗 protected void setUp() throws Exception { super.setUp(); Log.i(TAG, Utils.getMethodName() + "entry "); sdbApp = (SdbApplication) getContext().getApplicatio
protected void setUp() throws Exception {
super.setUp();
Log.i(TAG, Utils.getMethodName() + "entry ");
sdbApp = (SdbApplication) getContext().getApplicationContext();
mContext = getContext();
mProvider = new SdbContentProvider();
mProvider.attachInfo(mContext, null);
mMockContentResolver = new MockContentResolver();
// Create the authority for the URI, by removing the 'content://' and
// any
// '/' or path part after that.
String authority = SpaceDB.CONTENT_URI.toString().substring(10);
int pos = authority.indexOf('/');
if (pos > -1) {
authority = authority.substring(0, pos);
}
mMockContentResolver.addProvider(authority, mProvider);
authority = FolderDB.CONTENT_URI.toString().substring(10);
pos = authority.indexOf('/');
if (pos > -1) {
authority = authority.substring(0, pos);
}
mMockContentResolver.addProvider(authority, mProvider);
this.setContext(new IsolatedContext(mMockContentResolver, mContext));
Thread.sleep(5000);
}
这个
IsolatedContext
不应该帮我吗?即兴在setUp()
中复制数据库(使用getDatabasePath()
获取指向数据库的文件,然后从tearDown()中的副本还原它)
您应该实现回滚,以便在每个单独的测试之后发生回滚。这样,每个测试都可以被视为独立于其他测试。尝试以下方法:
public class DatabaseTests extends AndroidTestCase {
TestDatabaseHelper mDbHelper;
@Override
protected void setUp() throws Exception {
super.setUp();
... Create instance of TestDatbaseHelper under test here.
mDbHelper.getWritableDatabase().beginTransaction();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
mDbHelper.getWritableDatabase().endTransaction();
}
<!-- Add your unit tests in here -->
}
公共类数据库测试扩展了AndroidTestCase{
TestDatabaseHelper-mDbHelper;
@凌驾
受保护的void setUp()引发异常{
super.setUp();
…在此处创建测试中的TestDatbaseHelper实例。
mDbHelper.getWritableDatabase().beginTransaction();
}
@凌驾
受保护的void tearDown()引发异常{
super.tearDown();
mDbHelper.getWritableDatabase().endTransaction();
}
}