android - 使用 ServiceTestCase 访问 JUnit 的资源

标签 android service junit

有谁知道如何在从ServiceTestCase 扩展时访问JUnit 自己的项目res/ 文件夹编译的R 实例? ?

通常的方法行不通:

getInstrumentation().getContext().getResources()...

因为事实证明 ServiceTestCase 没有 getInstrumentation() 因为它没有扩展 InstrumentationTestCase方法。

提前致谢。

最佳答案

这个 hack 从我的 ServiceTestCase 子类中工作:

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

尽管欢迎使用更标准的方法。

关于android - 使用 ServiceTestCase 访问 JUnit 的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13415582/

相关文章:

带有通知 channel 的 Android 8 前台服务

android - 单元测试配置在 Android Studio 3.1 中损坏

android - TaskStackBuilder#startActivities() NullPointerException

android - 选项卡更改不起作用后使用新数据刷新 ListView (Android)

macos - 获取 Cocoa 中事件应用程序中当前选定的文本

java - 如何在 java 中使用 mockito 测试文件大小?

java - Android:执行单元测试时 Android 支持中的 IllegalStateException

android - 如何允许 View 超出设备尺寸 - android

android - Android 应用程序安装跟踪如何工作?

java - 使用 Intent.createChooser 并收到错误 : Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag