Android / Robolectric 框架 - 实例化活动在 getResource 上返回 null
这与使用 Robolectric 框架在 Android 上进行单元测试有关。我在正常运行时没有问题的代码上遇到空指针异常。我刚刚开始使用机器人电动车,所以它可能非常简单。
这是测试的调用代码:
@Test
public void testInitUtilsInitSequenceNumberIsRandom() {
// create an activity for reference
InitUtils initUtils = new InitUtils();
// do static initialization to parse questions into memory
InitUtils.initialize(initUtils); // <============ the call from roboelectric framework
// retreive app state
AppState appState = (AppState) initUtils.getApplicationContext();
// fill in later
fail("not implemented");
}
这是在 InitUtils 中调用的方法,该方法会崩溃
/** * 将 XML 加载到 {@see mQuestions} 类成员变量中 * */
public static void initializeQuestions(Activity activity, AppState appState) {
/* create XML Parser */
XmlResourceParser questionBatch;
/* local question variable */
Question question = null;
/* retrieve the XML for parsing */
// =============== This returns null ==============================
questionBatch = activity.getResources().getXml(R.xml.questions);
/* Parse the XML */
int eventType = -1;
/* iterate through XML */
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
/* Get the questions */
// ================================= NPE exception ======================
String strName = questionBatch.getName();
...etc
我需要做一些特殊的事情来检索资源吗?
This has to do with using the Robolectric framework for unit testing on android. I'm getting a null pointer exception on code which has no problem when running normally. I'm just starting on the roboelectric, so it's probably pretty simple.
Here is the calling code for Testing :
@Test
public void testInitUtilsInitSequenceNumberIsRandom() {
// create an activity for reference
InitUtils initUtils = new InitUtils();
// do static initialization to parse questions into memory
InitUtils.initialize(initUtils); // <============ the call from roboelectric framework
// retreive app state
AppState appState = (AppState) initUtils.getApplicationContext();
// fill in later
fail("not implemented");
}
Here is the method called within in InitUtils which crashes
/**
* Loads the XML into the {@see mQuestions} class member variable
*
*/
public static void initializeQuestions(Activity activity, AppState appState) {
/* create XML Parser */
XmlResourceParser questionBatch;
/* local question variable */
Question question = null;
/* retrieve the XML for parsing */
// =============== This returns null ==============================
questionBatch = activity.getResources().getXml(R.xml.questions);
/* Parse the XML */
int eventType = -1;
/* iterate through XML */
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
/* Get the questions */
// ================================= NPE exception ======================
String strName = questionBatch.getName();
...etc
Is there something special I need to do for this to retrieve the resource?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 Robolectric 的事情一无所知,但是 getResources() 返回 null 意味着它是在框架调用 Activity.onCreate() 之前被调用的。我不知道你从哪里得到这个 Activity,但如果你在 Instrumentation 之上进行单元测试,你需要确保你的 Instrumentation 线程阻塞,直到主线程完成执行,使用类似:
http://developer.android.com/reference/android/app/Instrumentation。 html#waitForIdleSync()
如果您使用 startActivitySync,这将为您完成:
http://developer.android.com/reference/android/app/Instrumentation.html#startActivitySync(android.content.Intent)
I don't know anything about this Robolectric thing, but getResources() returning null means it is being called before the framework has called Activity.onCreate(). I don't know where you got this Activity from, but if you are doing unit testing on top of Instrumentation you need to make sure that your instrumentation thread blocks until the main thread has finished executing, using something like:
http://developer.android.com/reference/android/app/Instrumentation.html#waitForIdleSync()
If you are using startActivitySync this will be done for you:
http://developer.android.com/reference/android/app/Instrumentation.html#startActivitySync(android.content.Intent)