Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android test raw resource

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?

like image 838
jlhonora Avatar asked Aug 24 '15 16:08

jlhonora


People also ask

Where is the resource raw folder in Android Studio?

The raw folder is created inside the res folder: main/res/raw. So we will simply create it inside the res folder. But before creating a raw folder let’s have a look at the asset folder in android which acts the same role as the raw folder in android studio. So let discuss how the resource raw folder is different from the assets folder?

How do I read raw files in Android?

However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the res/raw/ directory and read a stream of bytes using openRawResource (). Android contains a number of standard resources, such as styles, themes, and layouts.

What is the difference between @assets and Res/Raw in Android?

assets: No simple way the developer can arrange an XML file (e.g. AndroidManifest.xml) to point to the file in the assets folder. res/raw: In any XML files like in Java, the developer can access the file in res/raw using @raw/filename easily. So if you need to access your file in any XML, put it in the res/raw folder.

How to access androidtest resources without applicationid?

if you have androidTest resources in the right directory, then you can only access them via com.example.my.app.debug.test.R !!!! Even without applicationIdSuffix you should use < applicationId>.test.R in case applicationId is different than your package name. See Android Unit Tests Requiring Context.


2 Answers

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); 
like image 57
Jacob Ras Avatar answered Oct 05 '22 21:10

Jacob Ras


I had the androidTest resources in the right spot (src/androidTest/res) and I still couldn't access them via <normal.package>.test.R. I spent a lot of time googling trying to figure out what was going on..

I FINALLY stumbled onto the answer. If you're building a buildType where you specified an applicationIdSuffix, your files are at <applicationId><applicationIdSuffix>.test.R !!!!

i.e.

applicationId "com.example.my.app"  buildTypes {     debug {         applicationIdSuffix ".debug"     } } 

if you have androidTest resources in the right directory, then you can only access them via com.example.my.app.debug.test.R !!!!

like image 32
joshkendrick Avatar answered Oct 05 '22 20:10

joshkendrick