13

我正在使用 Robolectric 2 来测试我的活动。这扩展了 ActionBarActivity:

// This Activity uses one ActionBarCompat theme    
public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

我的测试是:

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {

    @Test
    public void shouldBlaBlaBla() throws Exception {
        Robolectric.buildActivity(MainActivity.class).create().get();
        assert(...);
    }
}

问题是当我告诉 Robolectric 创建 Activity 时,会引发异常。

java.lang.NullPointerException
    at java.util.regex.Matcher.getTextLength(Matcher.java:1234)
    at java.util.regex.Matcher.reset(Matcher.java:308)
    at java.util.regex.Matcher.<init>(Matcher.java:228)
    at java.util.regex.Pattern.matcher(Pattern.java:1088)
    at org.robolectric.res.ResName.<init>(ResName.java:25)
    at org.robolectric.res.builder.RobolectricPackageManager.getActivityInfo(RobolectricPackageManager.java:70)
    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.busuu.android.MainActivity.onCreate(MainActivity.java:31)
    at android.app.Activity.performCreate(Activity.java:5008)
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:117)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:124)
    at com.busuu.android.MainActivityTest.mainActivityShouldProvideASlidingMenu(MainActivityTest.java:16)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

根据这篇文章,我已将 @Config(reportSdk=10) 添加到我的测试中。结果是我得到的异常不一样了……这一次不是在创建Activity的时候发生的,而是在给Layout充气的时候(Activity使用了扩展@style/Theme.AppCompat的主题)

android.view.InflateException: XML file ./../appcompat/res/layout/abc_action_bar_decor_include.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.v7.internal.widget.ActionBarContainer
    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:805)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
    at android.app.Activity.setContentView(Activity.java:1867)
    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
    at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:175)
    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:133)
    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
    at com.busuu.android.MainActivity.onCreate(MainActivity.java:47)
    at android.app.Activity.performCreate(Activity.java:5008)
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:117)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:124)
    at com.busuu.android.MainActivityTest.mainActivityShouldProvideASlidingMenu(MainActivityTest.java:17)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:587)
    at android.view.LayoutInflater.createView(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:687)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_parseInclude(LayoutInflater.java:805)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:736)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:352)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at com.android.internal.policy.impl.PhoneWindow.$$robo$$PhoneWindow_1a87_setContentView(PhoneWindow.java:256)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
    at android.app.Activity.$$robo$$Activity_c57b_setContentView(Activity.java:1867)
    at android.app.Activity.setContentView(Activity.java)
    at android.support.v7.app.ActionBarActivity.$$robo$$ActionBarActivity_eab0_superSetContentView(ActionBarActivity.java:208)
    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java)
    at android.support.v7.app.ActionBarActivityDelegateBase.$$robo$$ActionBarActivityDelegateBase_c4c6_ensureSubDecor(ActionBarActivityDelegateBase.java:175)
    at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java)
    at android.support.v7.app.ActionBarActivityDelegateBase.$$robo$$ActionBarActivityDelegateBase_c4c6_setContentView(ActionBarActivityDelegateBase.java:133)
    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java)
    at android.support.v7.app.ActionBarActivity.$$robo$$ActionBarActivity_eab0_setContentView(ActionBarActivity.java:76)
    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java)
    at com.busuu.android.MainActivity.onCreate(MainActivity.java:47)
    at android.app.Activity.$$robo$$Activity_c57b_performCreate(Activity.java:5008)
    at android.app.Activity.performCreate(Activity.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:117)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:124)
    at com.busuu.android.MainActivityTest.mainActivityShouldProvideASlidingMenu(MainActivityTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    ... 21 more
Caused by: android.content.res.Resources$NotFoundException: can't find file for ResName{android:drawable/abc_ab_solid_dark_holo}
    at org.robolectric.shadows.Converter.convertAndFill(Converter.java:89)
    at org.robolectric.shadows.Converter.convertAndFill(Converter.java:50)
    at org.robolectric.shadows.ShadowResources.createTypedArray(ShadowResources.java:233)
    at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:208)
    at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:52)
    at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:465)
    at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
    at android.content.Context.obtainStyledAttributes(Context.java:374)
    at android.view.View.__constructor__(View.java:3297)
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    at org.robolectric.shadows.ShadowView.__constructor__(ShadowView.java:67)
    at android.view.View.<init>(View.java:3295)
    at android.view.ViewGroup.<init>(ViewGroup.java:427)
    at android.widget.FrameLayout.<init>(FrameLayout.java:101)
    at android.widget.FrameLayout.<init>(FrameLayout.java:97)
    at android.support.v7.internal.widget.ActionBarContainer.<init>(ActionBarContainer.java:55)
    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:805)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:736)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
    at android.app.Activity.setContentView(Activity.java:1867)
    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
    at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:175)
    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:133)
    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
    at com.busuu.android.MainActivity.onCreate(MainActivity.java:47)
    at android.app.Activity.performCreate(Activity.java:5008)
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:117)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:124)
    at com.busuu.android.MainActivityTest.mainActivityShouldProvideASlidingMenu(MainActivityTest.java:17)
    ... 21 more

我错过了什么,还是 Roboelectric2 不应该与 ActionBarCompat 一起工作?

4

1 回答 1

0

我也遇到过这个问题。

看起来版本 2.3-SNAPSHOT(在 sonatype 上可用)修复了它

于 2013-11-12T13:32:30.953 回答