Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:59:57+00:00 2026-05-31T17:59:57+00:00

I have setup an android test project that runs junit tests. It’s using two

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:59:58+00:00Added an answer on May 31, 2026 at 5:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JUnit test case in an Android project that contains code that
First to explain how projects are setup: Android Library project with two classes: UserActivity
I have a simple HelloWorld Activity that I try to test with an Android
I have some doubts regarding In-Application-Billing in android: Can i test it with different
i have used eclipse, android sdk and phonegap to create a small application. I
I have an android lib project, and an android test project i've built in
I'm writing JUnit test code for an android service implementation, and I have to
I have a weird problem in the setup of my Android application which is
I tried to use Proguard in my Android project, i setup Proguard using command
http://code.google.com/p/apps-for-android/ I have eclipse setup with Subversive plugin, I can download the codes down

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.