从Android Studio 2.4迁移到3.0时,Robolectric的大多数(也许全部)测试都会因下面的奇怪错误而失败。我们正在使用Robolectric 3.2.2。
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NullPointerException
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:332)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:259)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:41)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:199)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
at org.robolectric.res.ResourceParser.load(ResourceParser.java:57)
at org.robolectric.res.ResourceTableFactory.newResourceTable(ResourceTableFactory.java:21)
at org.robolectric.res.ResourceMerger.buildResourceTable(ResourceMerger.java:22)
at org.robolectric.RobolectricTestRunner.getAppResourceTable(RobolectricTestRunner.java:499)
at org.robolectric.RobolectricTestRunner.access$600(RobolectricTestRunner.java:41)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:327)
... 19 more
Caused by: java.lang.NullPointerException
at org.robolectric.res.DocumentLoader.loadFile(DocumentLoader.java:39)
at org.robolectric.res.DocumentLoader.load(DocumentLoader.java:30)
at org.robolectric.res.ResourceParser.load(ResourceParser.java:21)
... 24 more
对于Robolectric,还没有为Android Studio 3.0提供支持吗?
更新
崩溃发生在Robolectric DocumentLoader
类。
private void loadFile(FsFile dir, XmlLoader[] xmlLoaders) {
if (!dir.exists()) {
throw new RuntimeException("no such directory " + dir);
}
for (FsFile file : dir.listFiles(ENDS_WITH_XML)) {
loadResourceXmlFile(file, xmlLoaders);
}
}
当dir.listFiles(ENDS_WITH_XML)
时,它返回Null。这就是崩溃的原因。也许在最新的Android Studio上,资源没有生成XML?
更新
找到这个公关。也许这会解决问题,因为当它为空时,dir
不是目录。
https://github.com/robolectric/robolectric/pull/3158/files
更新
将问题报告给Robolectric https://github.com/robolectric/robolectric/issues/3216