Test data sources in Android unit testing

I want to test parsing of data returned from server.

I thought I might create several test XML files and then feed them to the sax parser which uses my custom data handler (as in the application that I test).

But where should I put those test XMLs?

I tried with [project_root]/res/xml, but then I get the error:

android.content.res.Resources$NotFoundException: Resource ID #0x7f040000 type #0x1c is not valid at android.content.res.Resources.loadXmlResourceParser(Resources.java:1870) at android.content.res.Resources.getXml(Resources.java:779)

Does that mean that the xml is invalid, or that android couldn't find the XML file? (I use the same XML that comes from the server - copied it from the firebug's net panel and pasted into the file).

Can I somehow read that XML as a text file, since getContext().getResources().getXml(R.xml.test_response1) returns XmlResourceParser instead of String (which I'd prefer)?


Asked by: Aston110 | Posted: 25-01-2022






Answer 1

Put you xmls in MyAppTest/assets, for example MyAppTest/assets/my.xml, and then read it using

InputStream myxml = getInstrumentation().getContext().getAssets().open("my.xml");

then you can invoke the parser with this stream.

Answered by: Miranda549 | Posted: 26-02-2022



Answer 2

Simply put the file in your current package (ie into a resource package) and use the class loader...

private InputStream getTestImage(String pathToFile) throws IOException
        {
            final ClassLoader loader = getClass().getClassLoader();
            assertNotNull(loader);
            final InputStream resourceAsStream = loader.getResourceAsStream(pathToFile);
            assertNotNull(resourceAsStream);
            return resourceAsStream;
        }

Answered by: Melissa563 | Posted: 26-02-2022



Similar questions

android - JNI sources in the APK

I have an Android project with an NDK/JNI library in Eclipse Ganymede. Inexplicably, the bin folder contains a copy of all my C++ sources from the jni folder. So does the APK, if you rename it to ZIP and examine. If I delete those files from bin, then clean and rebuild, they're there again. What going on, please? I have no intention of distributing the sources along with the app.


android - Is there a way to sign my app so the user doesn't have to check unknown sources?

I am not planning on releasing my app through the market, however I also do not want the user to have to check the unknown sources to be able to install it. Is there some way of signing it where I can avoid it or another work around?


android - How do I get the Java API sources?

Closed. This question does not meet Stack Overflow guid...


android - Map values between different data sources

Is there any chance to get a easy way to sync data, in this case contacts, between the android contentprovider and a JSON-based server? My problem is, that android uses the cursors and stuff and on the other side I have the JSON-Format. Second problem: the same value have now two different names, so I need a kind of mapping between values in the two different data sources. My first approach was to define a ...


android - Adding C++ sources with 'Link to file' option

I try to add some C/C++ sources into JNI folder by dragging from explorer and choosing 'Link to files' option from the File Operation dialog. How to add those sources into Android.mk makefile ? I tried combination LOCAL_SRC_FILES += source.cpp LOCAL_SRC_FILES += c:/full/path/to/file/source.cpp but nothing works. Each time error appears 'make: * No rule to make t...


java - Sources for an Android Image gallery view with pinch zoom, touch swipe etc

Closed. This question does not meet Stack Overflow guid...


android - None of my sources are being found

Okay, so I'm trying to get my app to work on my phone to see if it works. I'm noticing that the button that I want to make a phone call is not doing anything. Here's the code: public boolean onStart(Bundle savedInstanceState) { View callButton2 = null; callButton2.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { Intent i = new Intent(Intent.ACTION_CA...


android - Can I have two different sources pass data to an array adapter?

I am trying to add data from two different sources to an array adapter. One source is from a spinner containing hard coded strings, the other is to allow the user to create their own string to pass to the array (via the adapter). Here is my code below. It appears to me that the array adapter can only except one data source according to the arguments that can be passed to it.......ie ArrayAdapter<CharSequ...


Using sources from Github to Android Eclipse

How exactly do I use sources from Github? For example : https://github.com/ChristopheVersieux/HoloEverywhere Or https://github.com/emilsjolander/StickyListHeaders Or even


android - Sources not being found in Eclipse

I'm building an Android app with Eclipse and it all went well until I added a particular KeyEvent code. Once I tried to emulate my app, it crashed and died. I tried to clean it multiple times, but it is not being resolved. Also, when I try to edit the source path, the sources are shown for a split second but disappear. Here are the sources that are not being found: ActivityThread.performLaunchActivity(Activ...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top