Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 由于调用存储,Robolectric从NullPointerException失败_Android_Unit Testing_Junit_Robolectric - Fatal编程技术网

Android 由于调用存储,Robolectric从NullPointerException失败

Android 由于调用存储,Robolectric从NullPointerException失败,android,unit-testing,junit,robolectric,Android,Unit Testing,Junit,Robolectric,我正试图开始使用robolectric的单元测试,当运行空测试时,我会得到一个NullPointerException。在我的应用程序文件中,我初始化了缓存,它调用了系统存储,这导致robolectric崩溃。如能提供任何有关如何避免这种情况的信息,我们将不胜感激 这是堆栈跟踪 Java.lang.RuntimeException: java.lang.NullPointerException at org.robolectric.RobolectricTestRunner$2.eval

我正试图开始使用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)
编辑: 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);
    }

这是
robololectric
问题。我能够复制它,并在Github上创建了票证。我会跟进最新情况

关于代码的另一条注释-
context.getExternalCacheDir()
可以在卸载存储时返回
null
。你应该在使用前先检查一下

更新

埃里希·道格拉斯解决了这个问题。它没有发布,因此您应该使用
Robolectric 3.0-SNAPSHOT

看看我是怎么做到的

我不确定,但你是否添加了外部存储清单权限?是的,我的意思是,我的应用程序工作得很好。我只是想开始使用单元测试OK,对不起,不知道如何解决这个异常。让机器人分子工作有时可能会很痛苦。。。你没有考虑过全新的单元测试官方吗?我以为我在这么做,用robolectric作为单元测试的官方支持——这就是我获取信息的地方,我明白了。我还没有尝试过这个功能,但它应该是自给自足的,不需要Robolectric来工作。非常感谢您对我的代码进行研究和反馈