Android junit - 访问ServiceTestCase中的自定义共享首选项

时间:2014-02-13 02:24:28

标签: android sharedpreferences android-context junit3 servicetestcase

我想在ServiceTestCase中获取共享首选项:

SharedPreferences preferences = context.getSharedPreferences("preferences_name", Context.MODE_PRIVATE);

并做类似的事情:

int value = preferences.getInt("key", 0);
preferences.edit().putInt("key", ++value).commit();
int newValue = preferences.getInt("key", 0);
assertTrue(value != newValue);

然而,这不起作用。 测试失败了。

我尝试过不同的语境:

getContext();

getSystemContext();

以及

getContext().createPackageContext(this.getClass().getPackage().getName(), Context.CONTEXT_IGNORE_SECURITY);

最后

getSystemContext().createPackageContext(this.getClass().getPackage().getName(), Context.CONTEXT_IGNORE_SECURITY);

我做错了吗?

或者也许不可能在ServiceTestCase中获取自定义共享首选项文件?

如何在活动测试课程中实现这一目标?

0 个答案:

没有答案
相关问题