简体   繁体   English

Android Studio - 单元测试模拟应用重启

[英]Android Studio - Unit Tests Simulate App Restart

Hello I would like to use Unit Tests to see if my Data Save Structe is working the problem is, that I have no Idea how to Simulate a Restart programmatically.您好,我想使用单元测试来查看我的数据保存结构是否正常工作,问题是,我不知道如何以编程方式模拟重启。

Here is a Example Unit Test.这是一个示例单元测试。

(AppData is just a Class to save various Things like a Shopping List with multiple Entries.) (AppData 只是一个类,用于保存各种事物,例如具有多个条目的购物清单。)

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

What are some good practices, to deal with problems like this?有什么好的做法可以处理这样的问题?

Thank you in advance!先感谢您!

Restarting your app implies that you need to interact with the underlying Android operating system in order to test the correct behavior.重新启动您的应用意味着您需要与底层 Android 操作系统进行交互以测试正确的行为。 This means you need to write an Instrumented Test rather than a Unit Test.这意味着您需要编写仪器测试而不是单元测试。 In Android, we write Instrumented tests using the tools provided in the Testing Library in AndroidX .在 Android 中,我们使用AndroidX中的 测试库中提供的工具编写 Instrumented 测试。 In this case, you probably need to use UI Automator in order to interact with the device directly to stop your app and then restart it.在这种情况下,您可能需要使用UI Automator来直接与设备交互以停止您的应用程序,然后重新启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM