android - Robolectric 由于调用存储而从 NullPointerException 失败

标签 android unit-testing junit robolectric

我正在尝试开始使用 robolectric 进行单元测试,并且在运行空测试时出现 NullPointerException。在我的应用程序应用程序文件中,我初始化了我的缓存,它调用了系统存储,这导致 robolectric 崩溃。非常感谢任何有关如何规避这种情况的信息。

这是堆栈跟踪。

Java.lang.RuntimeException: java.lang.NullPointerException
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:228)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    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:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    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.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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
    at android.os.Environment.getStorageVolume(Environment.java:869)
    at android.os.Environment.isExternalStorageRemovable(Environment.java:756)
    at android.os.Environment.isExternalStorageRemovable(Environment.java:742)
    at com.*.*.ApplicationFile.getDiskCacheDir(ApplicationFile.java:131)
    at com.*.*.ApplicationFile.onCreate(ApplicationFile.java:87)

编辑: 获取磁盘缓存目录

public static File getDiskCacheDir(Context context, String uniqueName)
    {

        //Check if media is mounted or storage is built-in, if so, try and use external cache
        //otherwise use internal cache dir
        final String cachePath = Environment.MEDIA_MOUNTED
                .equals(Environment.getExternalStorageState()) ||
                !Environment.isExternalStorageRemovable() ? context.getExternalCacheDir()
                .getPath() :
                context.getCacheDir().getPath();

        return new File(cachePath + File.separator + uniqueName);
    }

最佳答案

这是 Robolectric 问题。我能够重现它并在 Github 上创建票证 https://github.com/robolectric/robolectric/issues/1674 .我会跟进更新。

关于您的代码的更多评论 - context.getExternalCacheDir() 可以在卸载存储时返回 null。使用前应先检查。

更新

此问题已由 Erich Douglass 修复。它尚未发布,因此您应该使用 Robolectric 3.0-SNAPSHOT。 看看我是怎么做到的https://github.com/emartynov/robolectic-experiments/blob/master/robolectric-3.0-test/build.gradle

关于android - Robolectric 由于调用存储而从 NullPointerException 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29596177/

相关文章:

java - 当用户选择今天之前的日期时,Android Java 显示错误消息

android - 如何在调试中使用 Eclipse 交互式控制台

java - 是否可以在 Android 应用程序上使用 MARY TTS 引擎?有没有人尝试过这个?从哪里开始比较好?

unit-testing - 使用 golang prometheus testutil 进行单元测试

java - 为什么我在模拟 TextView 时会收到 InvalidUseOfMatchersException?

Android - 无法设计支持 API 16 至 22 的 ActionBar

java - CLI 测试失败,传入 IDE - 加载资源

java - 将测试文件导入 JUnit 的简单方法

android - 使用 Kotlin 在 Android 中的测试中声明 @BeforeClass 的正确方法是什么

java - Android Studio 中的 Junit