我正在尝试使用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);
}
答案 0 :(得分:1)
问题是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