我有一个使用片段的Android应用程序,并已实现ActionBarCompat。我写了如下所示的简单Robolectric测试:
@Test
public void shouldNotBeNull() throws Exception {
MainFragment mainFragment = new MainFragment();
startFragment(mainFragment);
assertThat(mainFragment, notNullValue());
assertThat(mainFragment.getActivity(), notNullValue());
}
private void startFragment(MainFragment fragment) {
FragmentActivity activity = new FragmentActivity();
shadowOf(activity).callOnCreate(null);
shadowOf(activity).callOnStart();
shadowOf(activity).callOnResume();
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(fragment, null);
fragmentTransaction.commit();
}
我遇到以下异常:
java.lang.NullPointerException: null
at android.app.Activity.invalidateOptionsMenu(Activity.java:2595)
at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:30)
at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:572)
at android.support.v4.app.Fragment.setHasOptionsMenu(Fragment.java:708)
at com.example.android.ui.MainFragment.onCreate(MainFragment.java:599)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37)
at org.robolectric.shadows.ShadowLooper.post(ShadowLooper.java:198)
at org.robolectric.shadows.ShadowHandler.postDelayed(ShadowHandler.java:56)
at org.robolectric.shadows.ShadowHandler.post(ShadowHandler.java:51)
at android.os.Handler.post(Handler.java)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1322)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
at com.example.android.ui.MainFragmentTest.startFragment(MainFragmentTest.java:36)
at com.example.android.ui.MainFragmentTest.shouldNotBeNull(MainFragmentTest.java:22)
使用这样的东西时:
MainActivity activity = new MainActivity();
shadowOf(activity).callOnCreate(null);
shadowOf(activity).callOnStart();
shadowOf(activity).callOnResume();
其中MainActivity是MainFragment的持有者活动,我得到以下异常:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at org.robolectric.res.builder.RobolectricPackageManager.getActivityInfo(RobolectricPackageManager.java:59)
at android.support.v7.app.ActionBarActivityDelegate.getUiOptionsFromMetadata(ActionBarActivityDelegate.java:157)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:53)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at com.example.android.ui.MainActivity.onCreate(MainActivity.java:45)
at org.robolectric.shadows.ShadowActivity.invokeReflectively(ShadowActivity.java:176)
at org.robolectric.shadows.ShadowActivity.callOnCreate(ShadowActivity.java:121)
at com.example.android.ui.MainFragmentTest.startFragment(MainFragmentTest.java:28)
at com.example.android.ui.MainFragmentTest.shouldNotBeNull(MainFragmentTest.java:22)
我怀疑它可能与ActionBarCompat和Robolectric不兼容。任何帮助将不胜感激。
答案 0 :(得分:0)
从Robolectric 2.4开始,它应该支持Appcompat库。但是有一些问题,因为它没有开箱即用。帮助我的事情:
project.properties
文件如the config manual中所述:
target=android-19 android.library.reference.1=/../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/20.0.0
android.library.reference.2=/../../build/intermediates/exploded-aar/com.android.support/support-v4/20.0.0
android.library.reference.3=/../../build/intermediates/exploded-aar/com.viewpagerindicator/library/2.4.1
build.gradle
中为Robolectric设置排除显然Robolectric有自己版本的支持库,因此需要以下内容来防止可能的重复依赖(从here引用):
androidTestCompile('org.robolectric:robolectric:2.4') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'appcompat-v7' }