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 under test.
Next, the parseXml()
method is invoked using the recently obtained InputStream
. If there is an IOException
, the test will fail and spit out the error from the stack trace, and if everything goes well, we test that the result...