Testing data sources when testing Android devices

I want to check the parsing of data returned from the server.

I thought I could create some test XML files, and then pass them to the Sax parser, which uses my user data handler (as in the test application).

But where should I put these test XML files?

I tried with [project_root]/res/xml, but then I got 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 this mean that xml is invalid or that android could not find the XML file? (I use the same XML that comes from the server - I copied it from the netbug panel and pasted it into a file).

Can I somehow read this XML as a text file, as it getContext().getResources().getXml(R.xml.test_response1)returns XmlResourceParserinstead String(which would I prefer)?

+3
source share
2 answers

Put 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 call the parser with this thread.

+1
source

Just put the file in the current package (i.e. in the 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;
        }
+1
source

Source: https://habr.com/ru/post/1730210/


All Articles