【发布时间】:2016-10-17 13:39:32
【问题描述】:
您好,我在从资产中读取文件时遇到问题。每次我得到 NullPointerException。 我使用 Roboletric,并且在资产文件夹中有 onBoard.json(main/assets/onBoard.json 和 testAndroid/assets/onBoard.json) .这是我的测试类,带有打开文件的基本测试。
@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public class JsonUtilsTest extends Instrumentation {
@Test
public void readAssetsFileInAndroidTestContextTest() throws IOException {
ShadowApplication application = ShadowApplication.getInstance();
assertNotNull(application);
InputStream input = application.getApplicationContext().getAssets().open("onBoard.json");
assertNotNull(input);
}
@Test
public void strawberryTest() throws Exception {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("onBoard.json");
}
@Test
public void shouldGetJSONFromAssetTest() throws Exception{
assertNotNull(RuntimeEnvironment.application); //Getting the application context
InputStream input = RuntimeEnvironment.application.getAssets().open("onBoard.json");// the file name in asset folder
assertNotNull(input);
}
}
并记录消息:
java.lang.NullPointerException
at org.robolectric.shadows.ShadowAssetManager.open(ShadowAssetManager.java:179)
at android.content.res.AssetManager.open(AssetManager.java)
...
所有这些方法都返回 NullPointerException!这里是空指针InputStream。
你能给我一些建议吗?
非常感谢。
【问题讨论】:
-
assert 文件夹?????
-
对不起,我的意思是
assets -
这方面有什么更新吗?你找到解决办法了吗?
标签: android unit-testing robolectric