Loading a text file from AndroidTestProject
I have two projects, my main android application and another test project that runs on top of that android application.
I have placed a text file in my android test project, This file contains information xml content. The xml content is what my android application will receive when I hit my server.
I am using the TestCase class in my Android Test Project to test this bit of functionality. Unfortunately I'm having a problem loading the content to into my Android Test Project. Every time I try to load the file into my Android Test Project it does not find the te开发者_JS百科xt file. Am I right to say that the only way I can get to that file is by removing it from my Android Test Project and put it in my Actual Android Project or am I missing something that will let me load the File from my Android Test Project.
Details: In my Android Test Project I kept my file under /res/raw/testFile When I try doing
File myFile = new File("/res/raw/testFile");
I get a java.io.FileNotFoundException: /res/raw/testFile (No Such File or directory)
Thanks
I am using the TestCase class.
You cannot access system resource when you extend TestCase. TestCase is part of jUnit not android.
Instead you could extend InstrumentationTestCase. This would allow you to access the target application's context, getInstrumentation().getTargetContext()
, and the test application's context, getInstrumentation().getContext()
.
Then you can access your raw file as follows.
Context testContext = getInstrumentation().getContext();
InputStream input = testContext.getResources().openRawResource(R.raw.sample);
Check out this question if you need help working with the input stream.
As I know, resource of one application cannot be shared with the other unless you have made it as a content provider or so..
My suggetion is to write this file in some common place like /sdcard//sample.xml.
And read and write permission for both the apps.
Since Android Gradle Plugin
version 1.1 you haven't to use Instrumentation
to load file resource.
I wrote here how to do it with POJO unit test case.
精彩评论