Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

eclipse - Accessing resources in an android test project

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:

  • 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

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

Update:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
   }    
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...