Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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
Java Robolectric ShadowAssetManager空指针异常_Java_Android_Junit_Robolectric - Fatal编程技术网

Java Robolectric ShadowAssetManager空指针异常

Java Robolectric ShadowAssetManager空指针异常,java,android,junit,robolectric,Java,Android,Junit,Robolectric,这是我得到的错误: java.lang.NullPointerException at org.robolectric.shadows.ShadowAssetManager.open(ShadowAssetManager.java:161) at android.content.res.AssetManager.open(AssetManager.java) at dagrada.marco.shariki.MatrixFileReader.getMatrix(MatrixFileReader.

这是我得到的错误:

java.lang.NullPointerException
at org.robolectric.shadows.ShadowAssetManager.open(ShadowAssetManager.java:161)
at android.content.res.AssetManager.open(AssetManager.java)
at dagrada.marco.shariki.MatrixFileReader.getMatrix(MatrixFileReader.java:69)
at dagrada.marco.shariki.GameStatusHandler.loadLevel(GameStatusHandler.java:51)
at dagrada.marco.shariki.GameStatusHandler.loadGame(GameStatusHandler.java:38)
at test.GameStatusHandlerTest.testLoadGame(GameStatusHandlerTest.java:55)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
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:158)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
这是生成它的代码:

@RunWith(RobolectricTestRunner.class)
public class GameStatusHandlerTest {

@Mock
GraphicsRenderer renderer;


private GameStatusHandler handler;
private ArrayList<String> list = new ArrayList<>();
Context context;

@Before
public void setUp(){

    context = Robolectric.application.getApplicationContext();
    handler = new GameStatusHandler(context, renderer);


    list.add(0, "level0.txt");

}


@Test
public void testLoadGame(){
    try {
        handler.loadGame(list);
    } catch (Exception e) {
        e.printStackTrace();
    }

    assertTrue(handler.getCurrentLevel() == 0);
}
}

由于stacktrace没有深入,我想问题出在context.getAssets.open()方法中,但我无法找出原因,因为纯代码在模拟器和真实设备上都能完美运行


我可能在测试中遗漏了一些东西,我尝试将其作为junit测试运行。

如上所述,使用RobolectricGradleTestRunner而不是RobolectricTestRunner。 然后确保您正在读取的文件位于源代码的assets文件夹中:src/main/assets。 如果不想将其放在测试文件夹中,则必须将测试资产文件夹指定为gradle构建中的资产源之一,如下所示:

sourceSets.main {
    assets.srcDirs = ['src/main/assets', 'src/test/assets']
}
此外,由于您没有使用MockitoJUnitRunner类,因此您的模拟图形渲染器不会使用模拟注释进行初始化。您必须在setUp()中这样初始化它


请通过@Config注释在测试中指定android清单

确保使用
RobolectricGradleTestRunner
而不是
RobolectricTestRunner
。你的其他代码在哪里?什么是新的GameStatusHandler(上下文、渲染器)?自版本3.1.1以来,这不再是必需的,@JaredBurrows。请看@hick209,我在一年多前发布了这条评论。现在您可以使用
RobolectricTestRunner
,因为
RobolectricGradleTestRunner
已被弃用。RobolectricGradleTestRunner在3.1.1版@JaredBurrows中已被弃用。见stackoverflow.com/a/36564971/1848826
sourceSets.main {
    assets.srcDirs = ['src/main/assets', 'src/test/assets']
}
renderer = Mockito.mock(GraphicsRenderer.class);