Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following folder structure in Android Studio:

├── androidTest
│?? ├── java
│?? └── res
│??     └── raw
│??         └── test_file
└── main
    ├── java
    └── res
        └── raw
 ??         └── app_file

I'm trying to access the test_file resource which exists in the raw folder of the androidTest elements. Here's the code inside a Robotium test case that inherits from ActivityInstrumentationTestCase2:

InputStream is = this.getInstrumentation()
                 .getContext()
                 .getResources()
                 .openRawResource(R.raw.test_file);

Android Studio throws a reference error since the resource cannot be found. The exact error is "Cannot resolve symbol test_file".

How can I reference this resource form a test case, which exists on the androidTest resources bundle?

question from:https://stackoverflow.com/questions/32187233/android-test-raw-resource

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

1 Answer

By default your androidTest project will include your app's R class, but androidTest's resources will be generated into a separate file. Make sure you import the R class from your test project:

import com.your.package.test.R;

[..]

getInstrumentation().getContext().getResources().openRawResource(R.raw.test_file);

You can also directly reference the test project's R class:

getInstrumentation().getContext().getResources().openRawResource(com.your.package.test.R.raw.test_file);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...