使用Robolectric

时间:2015-05-28 14:02:36

标签: android robolectric

我试图用Robolectric 3.0-RC3对PreferenceFragment进行单元测试。我的偏好设置在preferences.xml文件中定义,有几个ListPreference

当我开始使用FragmentTestUtil.startFragment(fragment)测试我的片段时,方法addPreferencesFromResource(R.xml.preferences)会在onCreate的{​​{1}}方法中调用。

我得到以下例外:

引起:java.lang.RuntimeException:呵呵? ResName {com.myapp:阵列/ pageOrientation_entries}     在org.robolectric.shadows.Converter.convertAndFill(Converter.java:99)     在org.robolectric.shadows.Converter.convertAndFill(Converter.java:50)     at org.robolectric.shadows.ShadowResources.createTypedArray(ShadowResources.java:232)     at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:209)     在org.robolectric.shadows.ShadowResources.access $ 000(ShadowResources.java:55)     at org.robolectric.shadows.ShadowResources $ ShadowTheme.obtainStyledAttributes(ShadowResources.java:485)     在android.content.res.Resources $ Theme.obtainStyledAttributes(Resources.java)     在android.content.Context.obtainStyledAttributes(Context.java:472)     在android.preference.ListPreference。构造函数(ListPreference.java:49)     在android.preference.ListPreference。(ListPreference.java)     在android.preference.GenericInflater.createItem(GenericInflater.java:383)     在android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)     在android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)     在android.preference.GenericInflater.rInflate(GenericInflater.java:481)     在android.preference.GenericInflater.rInflate(GenericInflater.java:493)     在android.preference.GenericInflater.inflate(GenericInflater.java:326)     在android.preference.GenericInflater.inflate(GenericInflater.java:263)     在android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)     在android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:299)     在com.myapp.ui.settings.SettingsFragment.onCreate(SettingsFragment.java:53)     在android.app.Fragment.performCreate(Fragment.java:2031)     在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)     在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)     在android.app.BackStackRecord.run(BackStackRecord.java:833)     在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)     在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:447)     在android.os.Handler.handleCallback(Handler.java:739)     在android.os.Handler.dispatchMessage(Handler.java:95)     在org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)     在org.robolectric.shadows.ShadowMessageQueue.access $ 100(ShadowMessageQueue.java:29)     在org.robolectric.shadows.ShadowMessageQueue $ 1.run(ShadowMessageQueue.java:95)     在org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)     在org.robolectric.util.Scheduler.postAtFrontOfQueue(Scheduler.java:98)     在org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:114)     在android.os.MessageQueue.enqueueMessage(MessageQueue.java)     在android.os.Handler.enqueueMessage(Handler.java:631)     在android.os.Handler.sendMessageAtTime(Handler.java:600)     在android.os.Handler.sendMessageDelayed(Handler.java:570)     在android.os.Handler.post(Handler.java:326)     在android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)     在android.app.BackStackRecord.commitInternal(BackStackRecord.java:728)     在android.app.BackStackRecord.commit(BackStackRecord.java:704)     在org.robolectric.util.FragmentTestUtil.startFragment(FragmentTestUtil.java:14)     在com.myapp.ui.settings.SettingsFragmentTest.startFragment(SettingsFragmentTest.java:40)     在com.myapp.ui.settings.SettingsFragmentTest.plusFeaturesDisabled_onSignaturePrefClick(SettingsFragmentTest.java:70)     ......还有18个

当我评论所有PreferenceFragment并留下其他类型的偏好时,我不会遇到此问题。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

似乎Robolectric 3.0在处理通用数组资源方面存在问题,例如:

<array>
  <item>Option 1</item>
</array>

在我的情况下,我可以通过明确地将其更改为正确的数组类型来解决问题:

<string-array>
  <item>Option 1</item>
</string-array>