2017-03-19 18 views
1

DB:SQLite的如何在開始Espresso測試前準備DB數據?

表:聯繫

咖啡測試:

@Test 
public void testBlock() { 
    onData(anything()).inAdapterView(withId(R.id.container_ListView)).atPosition(0).onChildView(withText(R.string.block_user)) 
      .perform(click()); 
} 

和測試成功通過。但它只有在啓動聯繫之前具有狀態解除阻止(在db表中的列狀態)成功。因此,我需要(在開始測試之前)將此聯繫人更新爲狀態取消阻止。我怎樣才能做到這一點?或者有人有更好的解決方案嗎?

+0

你如何寫入數據庫?你有某種服務類或幫助者嗎? – stamanuel

+0

我有服務類使用com.j256.ormlite.dao.Dao與數據庫一起使用 – Alexei

回答

2

這是你可以在開始活動前在@Before方法或測試用例中完成的。

首先,你需要你的活動規則改成這樣:

@Rule 
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
     MainActivity.class, false, false); 

注意最後false標誌,這表示,你的活動不啓動時會自動測試開始。

然後在您的測試類,你可以添加這個方法:

@Before  
public static void PrepareDatabase() { 
    // Instantiate your Service that performs an action on the database 
    // give it this context: InstrumentationRegistry.getTargetContext(); 
    // For example the code could look like this: 
    DatabaseHelper(InstrumentationRegistry.getTargetContext()).setYourValue(); 
    mActivityRule.launchActivity(null); //launches the test activity 
} 

然後正常編寫測試。此代碼在數據庫上執行一些操作,然後啓動測試活動。您也可以將它傳遞到您的測試方法中,而不是在@Before方法中。

相關問題