如果存在AdMob视图,任何变通方法,Robolectric都无法使布局文件膨胀?

时间:2011-09-16 12:09:20

标签: android unit-testing admob android-2.1-eclair robolectric

这是我的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"/>

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

由于

1 个答案:

答案 0 :(得分:2)

我建议您在onCreate方法中查找admob广告的代码段,并将其放入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);

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