当我运行我的测试时,所有的东西都会保存在我的sqllite数据库中,所以我希望在我的Junit测试完成之后回滚我的Android数据库,这可能吗?
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
不应该帮我这个吗?
答案 0 :(得分:0)
关闭袖带,在setUp()
中获取数据库的副本(使用getDatabasePath()
获取File
指向您的数据库),然后从{{1}中的副本中恢复它}}
答案 1 :(得分:0)
您应该实现回滚,以便在每次单独测试后进行回滚。这样每个测试都可以被认为是独立于其他测试。尝试类似:
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 -->
}