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

标签 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 是否在测试 Artifact Unit Tests

最佳答案

我在更新 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 来开始我的类(class):

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

所以我注意到 Android Studio AndroidManifest 文件比单元测试高 1 级,所以我更改了过去的代码 fragment :

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

现在他们工作得很好。

关于Android 单元测试过去工作正常,现在抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232782/

相关文章:

android - LocalBroadcastReciever 不能在 fragment 中工作

java - 使用 Jackson 序列化空值

java - Swing 选项条件

android - 如何通过深度链接在不丢失 Activity 堆栈(或应用程序状态)的情况下恢复 Android 应用程序?

android - 如何将文件添加到 Android 项目,将其部署到设备,然后打开它?

android - 如何释放 TextToSpeech 的静态变量以避免泄漏?

Java按月份将月份数组排序为多个数组

android - 当应用程序自动启用它们时,如何在 native react 中获取应用程序权限的系统警报

android - 如何在android中使用日期和时间选择器,我有什么错误以及如何解决

android - 如何在android中的 fragment 布局中的按钮单击事件上动态添加textview