Robolectric NPE when migrate from Android Studio 2.4 to 3.0

Viewed 541

When migrating from Android Studio 2.4 to 3.0, most (perhaps All) of our test with Robolectric failing with the strange error below. We're using 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

Is it the support for Android Studio 3.0 is not ready yet for Robolectric?

UPDATE

The crash happens on Robolectric DocumentLoader class.

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);
 }
}

When dir.listFiles(ENDS_WITH_XML), it return Null. That's why it crash. Perhaps on the latest Android Studio, the resources are not generating XML?

UPDATE

Found this PR. Perhaps this would fix the issue, given when that's null, the dir is not a directory.

https://github.com/robolectric/robolectric/pull/3158/files

UPDATE

Report the issue to Robolectric https://github.com/robolectric/robolectric/issues/3216

0 Answers
Related