2013-06-04 47 views
0

當我運行我的測試時,所有的東西都保存在我的sqllite數據庫中,所以我想在我的Junit測試完成後回滾我的Android數據庫,這可能嗎?android testcase junit rollback

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); 
    } 

不應該這幫我這個嗎?

回答

0

關閉袖口,取出您的數據庫的副本setUp()(使用getDatabasePath()可獲得指向您數據庫的File),然後從tearDown()中複製副本。

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

}