How to access resources by ID in Android unit test?

I am trying to write a unit test that uses mock JSON responses.

Basically, I wanted to add json files to / res / raw and access them using the same method as in the application:

InputStream jsonStream = context.getResources().openRawResource(R.raw.mock_json_response) 

But when I do this, I get the following error:

 android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f060001 

I use robolectric for testing - I'm not sure if this will change the way you access external files.

I tried putting the JSON file in both /app/src/main/res and in /app/src/test/res , but it doesn't seem to work.

Is it possible to access resources in a test this way?

+5
source share

All Articles