尝试使用 Robolectric 测试 XML 解析时出错

Posted

技术标签:

【中文标题】尝试使用 Robolectric 测试 XML 解析时出错【英文标题】:Error trying to test XML parsing with Robolectric 【发布时间】:2014-04-19 13:41:44 【问题描述】:

我正在使用 Robolectric 并尝试测试解析 XML 响应。我收到以下异常:

java.lang.NoClassDefFoundError: java/lang/AutoCloseable
    at android.util.Xml.parse(Xml.java:80)
    at com.test.app.network.parser.soap.BaseSoapParser.doParse(BaseSoapParser.java:57)
    at com.test.app.network.parser.soap.RecipeParser.parse(RecipeParser.java:57)
    at com.test.app.network.command.RecipeCommand.searchRecipes(RecipeCommand.java:64)
    at com.test.app.data.PrepopulateDB.prepopulateDB(PrepopulateDB.java:53)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:230)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:172)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: java.lang.AutoCloseable
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.robolectric.bytecode.AsmInstrumentingClassLoader.loadClass(AsmInstrumentingClassLoader.java:100)
    at android.util.Xml.$$robo$$Xml_e8aa_parse(Xml.java:80)
    at android.util.Xml.parse(Xml.java)
    at com.test.app.network.parser.soap.BaseSoapParser.doParse(BaseSoapParser.java:57)
    at com.test.test.app.network.parser.soap.RecipeParser.parse(RecipeParser.java:57)
    at com.test.app.network.command.RecipeCommand.searchRecipes(RecipeCommand.java:64)
    at com.test.app.data.PrepopulateDB.prepopulateDB(PrepopulateDB.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    ... 22 more

问题是“AutoCloseable”仅在 Java 7 中添加,并且由于 Android 使用 Java 6,我可以理解为什么找不到该类 - 任何帮助将不胜感激。

图书馆:

robolectric-2.3-20140204.032758-112-jar-with-dependencies.jar

android-all-4.4_r1-robolectric-0.jar / android-all-4.3_r2-robolectric-0.jar

更新: 我使用 JDK 6 和 Target API 18 对此进行了测试,结果相同。

更新 2: 问题的根源似乎是这个类:org.apache.harmony.xml.ExpatReader 它正在使用 Java 6 或 Android API 18 上不可用的 AutoClosable。

【问题讨论】:

Looks to be a known issue with the dev 这实际上是问题的一半,我希望能够使用 Java 6 和 Android 4.3 做到这一点,但它也失败了。 把你的java平台升级到7级然后试试... @Jayesh 我尝试使用 Java7 不起作用。谢谢。 【参考方案1】:

其实这个接口在Android SDK里面:https://developer.android.com/reference/java/lang/AutoCloseable.html

在 API 级别 19 中添加

在 19 级之前,它存在但“隐藏”。

【讨论】:

这并不能解决问题,因为 Robolectric 不适用于 API 级别 19。AFAIK。【参考方案2】:

当您使用比某些所需类更高的 API 版本编译项目时会发生此错误,您的目标是 API 18,但正如 Guillaume 所说,当您使用 AutoCloseable 时,您正在尝试加载未添加的类直到 API 19。所以你的应用程序在运行时崩溃。您需要修改项目的 minSDK 版本或考虑替代方案。

【讨论】:

这不是一个应用程序,该应用程序在使用 API 级别 18 和 19 编译时可以完美运行。这是一个使用 Robolectric 的测试用例,此时失败。正如我在更新中所写的,当我使用 JDK 1.6 和 API 18 运行测试时也会发生这种情况——因此 Robolectric 引入了一些不兼容的文件——这可能是我同意的问题,我仍在寻找解决方案。 你能检查一下这个关于 Robolectric emulateSdk 功能的答案吗? ***.com/a/20848052/1668144或许对你有帮助 感谢您的帮助。这不起作用我认为这是为 API 级别 18 版本中的 xml 解析导入的类中的 Robolectric 错误。 (据我所知,它们与 API 级别不兼容。)IMO 这只能通过 Robolectric 不支持的 API 级别 19 或修复这些文件来解决,我在 Robolectric 上打开了一个问题。跨度> github.com/robolectric/robolectric/issues/810 似乎 Roboelectric 不支持 API 19,也 emulateSdk 仅适用于 API 级别 16 、 17 和 18。抱歉在之前的评论中建议。 IMO 您最好的选择是使用不同的测试仪。 试图说服后端将此功能切换为 JSON ;) 也是解决我的特定问题的一种方法(虽然不是这个问题)。【参考方案3】:

下载 SDK 包 [1],解压缩,然后更新/覆盖您的 Eclipse 和 Android SDK(只需事先备份 eclipse 和 android-sdk 文件夹)。 当然,之后在 SDK 和 Eclipse IDE 中进行所有必要的更新

[1]https://developer.android.com/sdk/index.html

【讨论】:

只是一个想法。我自己的项目也有类似的问题,更新到最新的 SDK/Eclipse 后它就消失了。值得一试 我是伏都教的忠实信徒,但它没有帮助。 :( 嗯,真可惜,您确定您使用的是最新的更新 adt jdk 等吗?查看以下链接了解更多信息:***.com/questions/20480090/…

以上是关于尝试使用 Robolectric 测试 XML 解析时出错的主要内容,如果未能解决你的问题,请参考以下文章

Robolectric:测试包含来自项目依赖的视图的活动

无法使用robolectric捕获HTTP请求

使用 Robolectric 测试 DialogFragments

Robolectric 单元测试中使用 Ressource

如何使用robolectric对Android音频录制应用进行单元测试

配置同时使用PowerMock和Robolectric对Android进行单元测试