I want to read from a file in my Instrumentation test:
public class MyTest extends AndroidTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
...
}
}
I tried following ways but non of them was successful:
InputStream inputStream = getContext().getAssets().open("myFile");
and File myFile = new File("src/androidTest/myFile");
I gave a try to
Resources res = getContext().getResources();
InputStream in = res.openRawResource(R.raw.testfile)
which is suggested in How to read a file from src/instrumentTest/resources directory in Android?, but I do not have access to R.
Do you know how can I do it?
I want to mock a FileInputStream
.
Addenda :
I found a workaround to get a inputStream:
InputStream inputStream = getInstrumentation()
.getContext()
.getResources()
.openRawResource(com.mypackage.instrumenttest.
R.raw.sifo_cookie_key_json);
The case is I need to extend InstrumentationTestCase
and not AndroidTestCase
to have access to R
But what I need is a fileInputStream
, and not inputStream since I need to have following mocking return:
when(mockContext.openFileInput(FILE_NAME)).thenReturn(fileInputStream);
And openFileInput :http://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String) returns fileInputStream
I made a workaround to convert InputStream to fileInputStream:
final File file = File.createTempFile("file", ".txt");
convertStreamToFile(inputStream, file);
FileInputStream fileInputStream = new FileInputStream(file);
But it is not a nice solution, and I wonder if I can get FileInputStream
instead of InputStream
?