andreich
andreich

Reputation: 1250

Robolectric: how fix FileNotFoundException from assets?

In my robolectric tests I am trying to read a file. Since this file contains just the test data I have put it in assets of the tests.

The directory structure looks like this. src/main/assets/prod_data_file src/test/assets/test_data_file

However when I try to read the file by getShadowApplication().getAssets().open(MY_FILE_PATH) or context.getAssets().open(MY_FILE_PATH) it gives me FileNotFoundException.

Does anybody know how to fix this?

I try this solution Using assets in robolectric tests

this not work https://stackoverflow.com/a/21465230/1654690

Upvotes: 1

Views: 1801

Answers (2)

Andrew Panasiuk
Andrew Panasiuk

Reputation: 656

This solution works for me:

  1. Move all text files to src/test/resources folder

  2. Get them with the following code

    getClass().getClassLoader().getResourceAsStream("path/inside/resource_folder");

This comment from Robolectric github helped me.

Upvotes: 1

Sudheesh Mohan
Sudheesh Mohan

Reputation: 2769

Make sure the path is correct. If you are sure the path is correct then try this ,

Right click the project > click Refresh. It may work. :)

Upvotes: 0

Related Questions