3

这是我启用 Robolectric 的 JUnit 测试类的一个片段:

@Before
    public void setUp() throws Exception
    {
        mActivity = new LanguageSelection();
        mActivity.onCreate(null);

        speakEnglishButton = (Button) mActivity.findViewById(com.jameselsey.apps.androidsam.R.id.speakEnglishButton);
        speakSpanishButton = (Button) mActivity.findViewById(com.jameselsey.apps.androidsam.R.id.speakSpanishButton);
        speakFrenchButton = (Button) mActivity.findViewById(com.jameselsey.apps.androidsam.R.id.speakFrenchButton);
        speakGermanButton = (Button) mActivity.findViewById(com.jameselsey.apps.androidsam.R.id.speakGermanButton);
        speakItalianButton = (Button) mActivity.findViewById(com.jameselsey.apps.androidsam.R.id.speakItalianButton);
    }

    @Test
    public void testButtonsVisible()
    {
        assertThat(speakEnglishButton.getVisibility(), equalTo(View.VISIBLE));
        assertThat(speakSpanishButton.getVisibility(), equalTo(View.VISIBLE));
        assertThat(speakFrenchButton.getVisibility(), equalTo(View.VISIBLE));
        assertThat(speakGermanButton.getVisibility(), equalTo(View.VISIBLE));
        assertThat(speakItalianButton.getVisibility(), equalTo(View.VISIBLE));
    }

运行我的测试时,该行mActivity.onCreate(null)会引发以下异常:

java.lang.RuntimeException: error inflating layout/languageselection
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:101)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:79)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:83)
    at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:33)
    at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:78)
    at com.xtremelabs.robolectric.RobolectricInternals.methodInvoked(RobolectricInternals.java:50)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at com.xtremelabs.robolectric.ShadowWrangler.methodInvoked(ShadowWrangler.java:78)
    at com.xtremelabs.robolectric.RobolectricInternals.methodInvoked(RobolectricInternals.java:50)
    at android.app.Activity.setContentView(Activity.java)
    at com.jameselsey.apps.androidsam.LanguageSelection.onCreate(LanguageSelection.java:28)
    at com.jameselsey.apps.androidsam.LanguageSelectionTest.setUp(LanguageSelectionTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:164)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.constructView(ViewLoader.java:161)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.create(ViewLoader.java:144)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:126)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:129)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:129)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:99)
    ... 51 more
Caused by: java.lang.UnsupportedOperationException
    at com.xtremelabs.robolectric.util.TestAttributeSet.getAttributeUnsignedIntValue(TestAttributeSet.java:69)
    at com.admob.android.ads.AdView.<init>(AdView.java:366)
    at com.admob.android.ads.AdView.<init>(AdView.java:308)
    at com.admob.android.ads.AdView.<init>(AdView.java:295)
    ... 61 more

languageselection.xml如果我从我的布局文件中注释掉以下视图,它工作得非常好:

<com.admob.android.ads.AdView
                android:id="@+id/ad"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="bottom"
                androidsam:backgroundColor="#000000"
                androidsam:primaryTextColor="#FFFFFF"
                androidsam:secondaryTextColor="#CCCCCC"/>

问题:对此有什么好的解决方法?我不想仅仅出于测试目的而禁用/启用广告。我不是在测试广告,但我不想为了测试其他元素(例如按钮)而删除它们。

谢谢

4

1 回答 1

2

我建议将代码片段移到您从方法中查找 admob 广告的onCreate位置,然后将它们放入onResume直接调用的方法中onCreate

// Look up the AdView as a resource and load a request.
AdView adView = (AdView) this.findViewById(R.id.adView);

// Request an advertisement
AdRequest request = new AdRequest();
request.setTesting(true); //This is used for testing on emulator. Does not affect production
adView.loadAd(request);

我不确定它为什么会爆炸,但它对我有用。

于 2011-10-07T09:55:55.720 回答