使用ServiceTestCase访问JUnit的资源

时间:2012-11-16 11:21:49

标签: android service junit

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

通常的方法不起作用:

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

因为结果ServiceTestCase没有getInstrumentation(),因为它没有扩展InstrumentationTestCase方法。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这个 hack 可以从我的ServiceTestCase子类中运行:

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

虽然欢迎采用更标准的方法。