0

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?

Community
  • 1
  • 1
Ali
  • 9,800
  • 19
  • 72
  • 152
  • Are you trying to read a real file, or create a mock file? – Jeff Bowman Jul 08 '15 at 16:34
  • A real file which I put in raw folder under resources. – Ali Jul 09 '15 at 08:23
  • In that case, you'll want to edit your question and tags to remove "mock". Unless you want a fake object with no real data (or limited real data), mocks and mocking frameworks like Mockito are irrelevant to your question. – Jeff Bowman Jul 09 '15 at 08:36
  • Actually, then, does [this question](http://stackoverflow.com/q/9898634/1426891) solve your problem? – Jeff Bowman Jul 09 '15 at 08:37
  • I will look into it and let you know. Thanks for your help. – Ali Jul 09 '15 at 08:47
  • @Jeff, I tried both solution in the link but for some reason both failed, but I found another solution which I have added in the Addenda. – Ali Jul 10 '15 at 13:35
  • Have you tried Robolectric? I test resources all the time without mocking anything. – Jared Burrows Jul 10 '15 at 13:47
  • @Jeff, I appreciate if you look at addenda and let me know if you have any suggestion. – Ali Jul 10 '15 at 13:47
  • Yes I did, but I want to test cookieManager and I face with another issue in Robolectric framework which I reported https://github.com/robolectric/robolectric/issues/1918, and wait for their answer. It is 10 days waiting now :) – Ali Jul 10 '15 at 13:51

0 Answers0