I implemented the JSON interface to get model data via http in one of my Android projects.
this still works, and I would like to write some tests. I created a test project, as suggested in the Android documentation. to test the JSON interface, I need some test data that I would like to put in a file.
In my research, it turned out that it is best to place these files in the resources folder of the Android testing project. To access the files in the resource folder, you need to extend the test class using InstrumentationTestCase. then it should be possible to access the files by calling getAssets (). open () for the resource object. so I came up with the following code:
public class ModelTest extends InstrumentationTestCase { public void testModel() throws Exception { String fileName = "models.json"; Resources res = getInstrumentation().getContext().getResources(); InputStream in = res.getAssets().open(fileName); ... } }
Unfortunately, I get the error "there is no such file or directory (2)" when trying to access the models.json file. (/assets/models.json)
when getting a list of available files using
String[] list = res.getAssets().list("");
"models.json" is listed here.
I run these tests at Android 4.2.2 api 17 level.
source share