Android InstrumentationTestCase GetFilesDir() Returns Null


Answer :

I think that you are right with keeping your test data separate from tested application.

You can fix problem with Null by creating files directory for Instrumentation app by executing the following commands

adb shell cd /data/data/<package_id_of_instrumentation_app> mkdir files 

You can do above only on emulator or rooted device.

Then test from your question will not fail. I did it and also uploaded file named tst.txt to files dir, all below tests were successful:

assertNotNull(getInstrumentation().getContext().getFilesDir()); assertNotNull(getInstrumentation().getContext().openFileInput("tst.txt")); assertNotNull(getInstrumentation().getContext().openFileOutput("out.txt", Context.MODE_PRIVATE)); 

But I think more convenient way to provide data to test project is to use assets of test project where you can simply save some files and open them:

assertNotNull(getInstrumentation().getContext().getAssets().open("asset.txt")); 

or if you want to save some results of tests to the file you can use ExternalStorage:

File extStorage = Environment.getExternalStorageDirectory(); assertNotNull(extStorage); 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable