eclipse – Accessing resources in an android test project-ThrowExceptions

Exception or error:

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(
at android.content.res.Resources.openRawResource(
at com.quizzer.test.QuestionHandlerTests.testImportQuestions(
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(
at android.test.AndroidTestRunner.runTest(
at android.test.InstrumentationTestRunner.onStart(
Caused by: Hello World, HelloAndroidActivity!
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(
at android.content.res.Resources.openRawResource(
... 14 more

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.

How to solve:

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


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


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


I derived the test case as follows:

class MyTest extends InstrumentationTestCase {
    void setUp() {
        InputStream is = getInstrumentation().getContext().getAssets()

And the file test_image.bmp is saved in assets directory, which is reasonable if you intend to use the asset for some testing related work – and its not part of ui resources. The technique is used in another context here:


With build tools 3.0.0 you can use ActivityTestRule

class MainActivityTest {
    var mainActivityRule = ActivityTestRule(

    private val baseUrl: String
        get() {
            return mainActivityRule.activity.getString(R.string.base_url)

    fun launchingWithMovieIntent() {
            assert.that(baseUrl, equalTo("someValue")


Since Android Gradle Plugin version 1.1 you haven’t to use Instrumentation to load file resource.

I wrote here how to do it with POJO unit test case.

Leave a Reply

Your email address will not be published. Required fields are marked *