开发者

Robolectric fails to inflate layout file if an AdMob view is present, any workarounds?

This is a snippet from my Robolectric enabled JUnit test class :

@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));
    }

The line mActivity.onCreate(null) throws the following exception when my tests are run :

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
开发者_运维百科

If I comment out the following view from my languageselection.xml layout file, it works perfectly fine :

<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"/>

Question : What is a good work around for this? I don't want to have to disable/enable ads purely for testing purposes. I'm not testing the ads, but I don't want to have to remove them for testing of other elements such as buttons.

Thanks


I would recommend moving the code snippet where you look up the admob ads from the onCreate method and place them into the onResume method which gets called straight after 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);

I'm not sure why it's blowing up but it works for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜