相关疑难解决方法(0)

访问android测试项目中的资源

我已经设置了一个运行junit测试的android测试项目.它正在使用两个eclipse项目"Application"和"ApplicationTest",我的测试在"ApplicationTest"项目中.在我的一个测试中,我需要访问一个文件,如果我把文件放在SD卡上并指向一个File对象,这个工作正常.但是我想将该文件作为资源访问,但这似乎不起作用.这就是我做的:

  • 保存文件 ApplicationTest/res/raw/myfile.xml
  • 试图使用它: InputStream is = getContext().getResources().openRawResource(R.raw.myfile);

但这给了我这个例外:

android.content.res.Resources$NotFoundException: File Hello World, HelloAndroidActivity! from drawable resource ID #0x7f040000
at android.content.res.Resources.openRawResource(Resources.java:823)
at android.content.res.Resources.openRawResource(Resources.java:799)
at com.quizzer.test.QuestionHandlerTests.testImportQuestions(Tests.java:182)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
Caused by: java.io.FileNotFoundException: Hello World, HelloAndroidActivity!
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:406)
at android.content.res.Resources.openRawResource(Resources.java:820)
... 14 more

我的测试类扩展了AndroidTestCase,这就是上下文的来源.

更新:

所以问题似乎是在编译期间使用了测试项目中的资源,但是在运行时使用了主项目中的资源.我还不确定如何解决这个问题.因此,目前只有在测试项目和主项目中放置相同的原始资源时才有效,这当然是非常愚蠢的.

eclipse android unit-testing

19
推荐指数
1
解决办法
2万
查看次数

尝试访问raw文件夹中的资源时,Robolectric抛出Resources $ NotFoundException

当我尝试访问原始资源时收到android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000错误.

该资源是.bks证书,用于服务器验证.它存储在res/raw文件夹中.

最尴尬的是,当我将证书(和验证架构)复制到一个新项目(我为了测试它而构建)时,它起作用了!

我正在使用nenick的自定义运行器,因此Robolectric可以正确找到清单,资源和资产的文件夹.当我转到intermediates/res/debug文件夹时,所有资源都在那里.我没有项目中的风味(项目曾经有过,我删除了它们.也许这就是问题?)

我正在使用com.android.tools.build:gradle:1.2.2.buildToolsVersion '22.0.1'.我对该项目有很多依赖:(

另外,我正在使用testCompile 'org.robolectric:robolectric:3.0-rc2'testCompile 'org.robolectric:shadows-support-v4:3.0-rc2' 堆栈跟踪:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060000
    at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:346)
    at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:385)
    at android.content.res.Resources.openRawResource(Resources.java)
    at com.xxx.xxx.xxx.ssl.CertificateHelper.loadKeyStoreFromRaw(CertificateHelper.java:145)
    at com.xxx.xxx.xxx.xxx.XXX.createSSLSocketFactory(XXX.java:432)
    at com.xxx.xxx.xxx.xxx.xxx.XXX.<init>(XXX.java:92)
    at com.xxx.xxx.xxx.xxx.xxx.XXX.initiate(XXX.java:73)
    at com.xxx.xxx.xxx.MainApplication.onCreate(MainApplication.java:60)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:131)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:431)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:224)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) …
Run Code Online (Sandbox Code Playgroud)

android robolectric android-resources

9
推荐指数
1
解决办法
3468
查看次数