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

Robolectric fails from NullPointerException because of calls to storage

我正在尝试使用 robolectric 进行单元测试,并且在 运行 甚至是空测试时出现 NullPointerException。在我的应用程序应用程序文件中,我初始化了我的缓存,它调用了导致 robolectric 崩溃的系统存储。任何有关如何规避此问题的信息将不胜感激。

这是堆栈跟踪。

Java.lang.RuntimeException: java.lang.NullPointerException
    at org.robolectric.RobolectricTestRunner.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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)

编辑: getDiskCacheDir

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() 可以 return null 卸载存储时。使用前应先检查一下。

更新

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