[英]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). 我想我可能会创建几个测试XML文件,然后将它们提供给使用我的自定义数据处理程序的sax解析器(就像我测试的应用程序一样)。
But where should I put those test XMLs? 但是我应该在哪里放置那些测试XML?
I tried with [project_root]/res/xml
, but then I get the error: 我尝试了
[project_root]/res/xml
,但后来我收到错误:
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? 这是否意味着xml无效,或者android无法找到XML文件? (I use the same XML that comes from the server - copied it from the firebug's net panel and pasted into the file).
(我使用来自服务器的相同XML - 从firebug的网络面板复制并粘贴到文件中)。
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)? 我可以以某种方式将XML作为文本文件读取,因为
getContext().getResources().getXml(R.xml.test_response1)
返回XmlResourceParser
而不是String
(我更喜欢)?
Put you xmls in MyAppTest/assets
, for example MyAppTest/assets/my.xml
, and then read it using 将xmls放在
MyAppTest/assets
,例如MyAppTest/assets/my.xml
,然后使用它读取它
InputStream myxml = getInstrumentation().getContext().getAssets().open("my.xml");
then you can invoke the parser with this stream. 然后你可以用这个流调用解析器。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.