Problem with assets


Last Updated:

  1. zorglub76

    zorglub76 New Member This Topic's Starter

    Joined:
    Jan 29, 2010
    Messages:
    2
    Likes Received:
    0
    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 (Text):
    1. getContext().getAssets().open("test_response1.xml");
    but I'm getting the "file not found" exception.

    I tried to list assets using
    Code (Text):
    1. 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...
     

    Advertisement
  2. Emini

    Emini Member

    Joined:
    Oct 1, 2009
    Messages:
    6
    Likes Received:
    0
    Hmm, that works for me.

    The only difference I see is that I use the getResources() from the activity and call
    Code (Text):
    1. this.getResources().openRawResource(R.raw...);
    You might try that.
     
  3. zorglub76

    zorglub76 New Member This Topic's Starter

    Joined:
    Jan 29, 2010
    Messages:
    2
    Likes Received:
    0
    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....
     
  4. pzulw

    pzulw New Member

    Joined:
    Mar 31, 2010
    Messages:
    3
    Likes Received:
    0
    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.
     
  5. pzulw

    pzulw New Member

    Joined:
    Mar 31, 2010
    Messages:
    3
    Likes Received:
    0

Share This Page

Loading...