这是我得到的错误:
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 测试运行。
最佳答案
如上所述,使用 RobolectricGradleTestRunner 而不是 RobolectricTestRunner。 然后确保您正在阅读的文件存在于源代码的 Assets 文件夹中:src/main/assets。 如果您不想将它放在您的测试文件夹中,那么您必须将测试 Assets 文件夹指定为您的 gradle 构建中的 Assets 源之一,如下所示:
sourceSets.main {
assets.srcDirs = ['src/main/assets', 'src/test/assets']
}
您的模拟 GraphicsRenderer 也不会使用 Mock 注释进行初始化,因为您没有使用 MockitoJUnitRunner 类。您必须在 setUp() 中像这样初始化它。
renderer = Mockito.mock(GraphicsRenderer.class);
关于java - Robolectric Shadow Asset Manager 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31792317/