Android:如何在单元测试期间重置/清除应用程序首选项?
我想从一致的测试环境开始,因此我需要重置/清除我的首选项。这是迄今为止我进行的测试设置。它没有报告任何错误,并且我的测试通过了,但首选项没有被清除。
我正在测试“MainMenu”活动,但我暂时切换到 OptionScreen 活动(它扩展了 Android 的 PreferenceActivity 类)。我确实看到测试在运行期间正确打开了 OptionScreen。
public class MyTest extends ActivityInstrumentationTestCase2<MainMenu> {
...
@Override
protected void setUp() throws Exception {
super.setUp();
Instrumentation instrumentation = getInstrumentation();
Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(OptionScreen.class.getName(), null, false);
StartNewActivity(); // See next paragraph for what this does, probably mostly irrelevant.
activity = getActivity();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
settings.edit().clear();
settings.edit().commit(); // I am pretty sure this is not necessary but not harmful either.
StartNewActivity 代码:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(instrumentation.getTargetContext(),
OptionScreen.class.getName());
instrumentation.startActivitySync(intent);
Activity currentActivity = getInstrumentation()
.waitForMonitorWithTimeout(monitor, 5);
assertTrue(currentActivity != null);
谢谢!
I want to start with a consistent test environment so I need to reset/clear my preferences. Here's the SetUp for test I have so far. It's not reporting any errors, and my tests pass, but the preferences are not being cleared.
I'm testing the "MainMenu" activity, but I temporarily switch to the OptionScreen activity (which extends Android's PreferenceActivity class.) I do see the test correctly open the OptionScreen during the run.
public class MyTest extends ActivityInstrumentationTestCase2<MainMenu> {
...
@Override
protected void setUp() throws Exception {
super.setUp();
Instrumentation instrumentation = getInstrumentation();
Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(OptionScreen.class.getName(), null, false);
StartNewActivity(); // See next paragraph for what this does, probably mostly irrelevant.
activity = getActivity();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
settings.edit().clear();
settings.edit().commit(); // I am pretty sure this is not necessary but not harmful either.
StartNewActivity Code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(instrumentation.getTargetContext(),
OptionScreen.class.getName());
instrumentation.startActivitySync(intent);
Activity currentActivity = getInstrumentation()
.waitForMonitorWithTimeout(monitor, 5);
assertTrue(currentActivity != null);
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有从 edit() 调用中保存原始编辑器,并且您获取了编辑器的新实例并在其上调用 commit() ,而没有对该编辑器进行任何更改。试试这个:
The problem is that you aren't saving the original editor from the edit() call, and you fetch a new instance of the editor and call commit() on that without having made any changes to that one. Try this:
答案就在这里,
android 单元测试:在测试活动之前清除首选项
致电,
Answer is here,
android unit test: clearing prefs before testing activity
Call,