Android单元测试过去工作正常,现在抛出NullPointerException

时间:2016-02-05 20:18:27

标签: android nullpointerexception android-manifest robolectric android-testing

所以我有一些类是我的模型的单元测试,在更新Android Studio后,我的单元测试不再有效我得到以下消息,任何人都知道它可能意味着什么?

java.lang.NullPointerException
    at org.robolectric.manifest.MetaData.init(MetaData.java:55)
    at org.robolectric.manifest.AndroidManifest.initMetaData(AndroidManifest.java:343)
    at org.robolectric.res.builder.DefaultPackageManager.addManifest(DefaultPackageManager.java:417)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:74)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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:152)
    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:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

java.lang.RuntimeException: java.lang.NullPointerException
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:244)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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:152)
    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:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
    at org.robolectric.manifest.MetaData.init(MetaData.java:55)
    at org.robolectric.manifest.AndroidManifest.initMetaData(AndroidManifest.java:343)
    at org.robolectric.res.builder.DefaultPackageManager.addManifest(DefaultPackageManager.java:417)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:74)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    ... 18 more

java.lang.NullPointerException
    at org.robolectric.manifest.MetaData.init(MetaData.java:55)
    at org.robolectric.manifest.AndroidManifest.initMetaData(AndroidManifest.java:343)
    at org.robolectric.res.builder.DefaultPackageManager.addManifest(DefaultPackageManager.java:417)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:74)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    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:152)
    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:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
java.lang.NullPointerException
    at org.robolectric.manifest.MetaData.init(MetaData.java:55)
    at org.robolectric.manifest.AndroidManifest.initMetaData(AndroidManifest.java:343)
    at org.robolectric.res.builder.DefaultPackageManager.addManifest(DefaultPackageManager.java:417)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:74)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    at org.robolectric.R
Process finished with exit code -1

我已检查Build Variants是否在测试工件Unit Tests

1 个答案:

答案 0 :(得分:2)

更新Android Studio后,这也发生在我身上。如果您注意到前两行错误,则表示metadataAndroidManifest正在抛出NPE:

java.lang.NullPointerException
    at org.robolectric.manifest.MetaData.init(MetaData.java:55)
    at org.robolectric.manifest.AndroidManifest.initMetaData(AndroidManifest.java:343)

所以我使用Roboelectric开始我的课程:

@Config(sdk = 16, manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class MyClassTest{
  //tests class here
 }

所以我注意到Android Studio AndroidManifest文件比单元测试高1级,因此我将过去的片段改为:

@Config(sdk = 16, manifest = "../src/main/AndroidManifest.xml") //notice ../ added at beggining

现在他们工作得很好。