0

当我运行我的测试时,所有的东西都保存在我的 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帮助我吗?

4

2 回答 2

0

You should implement a rollback so that it occurs after each individual test. That way each test can be considered independent of the others. Try something like:

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 -->

}
于 2013-06-04T17:40:43.280 回答
0

即兴发挥,获取数据库的副本setUp()(用于getDatabasePath()获取File指向数据库的指向),然后从tearDown().

于 2013-06-04T16:37:24.893 回答