The parser test

This test implements an AndroidTestCase as all we need is a Context to be able to reference our assets folder. Also, we have written the parsing inside of the test, as the point of this test is not how to parse xml but how to reference mock assets from your tests:

public class ParserExampleActivityTest extends AndroidTestCase {

 public void testParseXml() throws IOException {
 InputStream assetsXml = getContext().getAssets()
.open("my_document.xml");

  String result = parseXml(assetsXml);
  assertNotNull(result);
 }
}
}

The InputStream class is obtained by opening the my_document.xml file from the assets by getContext().getAssets(). Note that the Context and thus the assets obtained here are from the tests package and not from the Activity ...

Get Learning Android Application Testing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.