Robolectric ShadowAssetManager nullpointer异常

时间:2015-08-03 16:28:24

标签: 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: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);
}

通过stactrace,它清楚导致此错误的代码是对此方法的调用,这是MatrixFileReader类中的静态方法。

public static int[][] getMatrix(Context context, String name) throws Exception {

    int[][] marbles = getMatrix(context.getAssets().open(name));
    return marbles;
}

}

由于堆栈跟踪没有深入,我猜问题出现在context.getAssets.open()方法中,但我无法弄清楚原因,因为纯代码运行完美在模拟器和真实设备上。

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

2 个答案:

答案 0 :(得分:1)

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

sourceSets.main {
    assets.srcDirs = ['src/main/assets', 'src/test/assets']
}

同样,您的模拟GraphicsRenderer不会使用Mock注释进行初始化,因为您没有使用MockitoJUnitRunner类。你必须在setUp()中初始化它。

renderer = Mockito.mock(GraphicsRenderer.class);

答案 1 :(得分:0)

请通过@Config注释将android清单指定给您的测试