• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with assets

zorglub76

Lurker
Hi all

I can't seem to reach assets.

I'm creating unit tests for parsing of data that is returned from server. So I wanted to create several xmls and use them in tests (instead of going to server).

I've put an xml file in [project_root]/assets directory and tried to get it with:
Code:
getContext().getAssets().open("test_response1.xml");
but I'm getting the "file not found" exception.

I tried to list assets using
Code:
getContext().getAssets().list("/");
but I was only getting several files and dirs from the root of my project (among which there was "assets" directory). Then I listed "/assets" path, but it was empty...

Any ideas??

P.S. I also tried putting the file in [project_root]/res/raw directory, and then get it with openRawResource(R.raw.test_response1), but I only got null pointer exception...
 
Hmm, that works for me.

The only difference I see is that I use the getResources() from the activity and call
Code:
this.getResources().openRawResource(R.raw...);

You might try that.
 
Nope. Still doesn't work...

I don't know what causes this... I've created a new "Android Test Project", created a new class that extends AndroidTestCase, I'm running it as "Android JUnit Test"...
With AndroidTestCase you have to get context before getting the resources, so i need
getContext().getResources()...
Maybe that is causing troubles - maybe it's the wrong context, or something...
Or maybe these asset or res/raw files are not sent to emulator when project is built.. I ran out of ideas - I'm trying to solve this problem for two days now....
 
One thing that was not clear to me. AndroidTestCase.getContext() returns the context of the application you are testing (MyApp), not the context of the Android Test Application (MyAppTest).

So you may not realize you are accessing the assets of the target app.

If you're putting test data in assets of the Android Test Application (sensible) then there must be some other way to access it....

...don't know the answer yet.
 
Back
Top Bottom