您好,我想使用单元测试来查看我的数据保存结构是否正常工作,问题是,我不知道如何以编程方式模拟重新启动。
这是一个示例单元测试。
(AppData 只是一个类,用于保存各种事物,例如具有多个条目的购物 list 。)
@Test
public void getDataAfterRestart(){
//Save a Entry
AppData appData = new AppData();
appData.addShoppingEntry(new ShoppingEntry("Bread"));
appData.save();
//************************
//Restart the Application*
//************************
//After the App reopend it self check if the Entry is still here
int entries = appData.getShoppingEntries().size();
assertEquals(1,entries);
}
处理此类问题有哪些好的做法?
提前谢谢您!
最佳答案
重新启动您的应用意味着您需要与底层 Android 操作系统交互才能测试正确的行为。这意味着您需要编写仪表测试而不是单元测试。在 Android 中,我们使用 Testing Library in AndroidX 中提供的工具编写仪器测试。 。在这种情况下,您可能需要使用 UI Automator以便直接与设备交互以停止您的应用程序,然后重新启动它。
关于java - Android Studio - 单元测试模拟应用程序重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60495623/