访问资源在Android测试项目测试、项目、资源、Android

2023-09-13 23:48:25 作者:踩着棺材跳鬼步

我已经安装运行JUnit测试一个Android测试项目。它使用两个Eclipse项目应用和ApplicationTest在我的测试是在ApplicationTest项目。在我的测试之一,我需要访问一个文件,这个工作正常,如果我把文件的SD卡和使File对象指向它。不过,我想访问该文件作为资源,但似乎并没有工作。这是我做的:

I have setup an android test project that runs junit tests. It's using two eclipse projects "Application" and "ApplicationTest" where my tests are in the "ApplicationTest" project. In one of my tests I need to access a file, this works fine if I put the file on the sdcard and point a File object to it. However I would like to access the file as a resource, but that does not seem to work. This is what I did:

保存在文件中 ApplicationTest / RES /生/ myfile.xml中 好一会才使用:的InputStream是=的getContext()getResources()openRawResource(R.raw.myfile); Saved the file in ApplicationTest/res/raw/myfile.xml Trying to get it using: InputStream is = getContext().getResources().openRawResource(R.raw.myfile);

但是,这给了我这个异常:

But that gives me this exception:


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所以这就是上下文的来源。

My test class extends AndroidTestCase so that's where the context comes from.

更新:

所以,问题似乎是,编译期间在测试项目中的资源的使用,但在运行时在主项目的资源的使用。我还没有不知道该如何解决这个问题。所以目前它只能如果我把同样的原料资源均是在试飞项目,当然这是相当愚蠢的主要项目。

So the problem seem to be that during compilation the resources in the test project are used, but at runtime the resources in the main project are used. I am yet unsure how to fix that. So currently it only works if I put the same raw resource both in the test project and the main project which of course is quite stupid.

推荐答案

我会建议延长 ActivityTestCase而不是 AndroidTestCase 。你可以通过

I would suggest extending ActivityTestCase instead of AndroidTestCase. You can than access test project resources via

getInstrumentation().getContext().getResources().openRawResource(R.raw.your_res).

假人的测试情况下,例如:

Dummy test case example:

public class Test extends ActivityTestCase {

   public void testFoo() {  

      // .. test project environment
      Context testContext = getInstrumentation().getContext();
      Resources testRes = testContext.getResources();
      InputStream ts = testRes.openRawResource(R.raw.your_res);

      assertNotNull(testRes);
   }    
}

然后在测试方法使用 getInstrumentation()。getTargetContext(),无论你使用的getContext()在你的 AndroidTestCase 扩展。

And then in test methods use getInstrumentation().getTargetContext() wherever you used getContext() in your AndroidTestCase extension.