Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric fails to inflate layout file if a custom view is present

This is how my test method looks like:

@RunWith(RobolectricTestRunner.class)
public class TestMyApplication{
    private About aboutActivity;

    @Test
    public void AboutActivityTest() throws Exception{

        aboutActivity = new About();
        aboutActivity.onCreate(null);

        TextView aboutInfo = (TextView) sobreActivity.findViewById(R.id.text_version);
        assertThat(sobreInfo.getText().toString(), equalTo("My Application v1.0"));
    }

}

The line aboutActivity.onCreate(null); throws the following exception when I run this test

java.lang.RuntimeException: error inflating layout/about
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:106)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
    at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:336)
    at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
    at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:102)
    at android.app.Activity.setContentView(Activity.java)
    at br.com.About.onCreate(About.java:22)
    at br.com.MyApplication.AboutActivityTest(MyApplication.java:48)
    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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:269)
    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.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(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.constructView(ViewLoader.java:178)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.create(ViewLoader.java:158)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:133)
    at com.xtremelabs.robolectric.res.ViewLoader$ViewNode.inflate(ViewLoader.java:136)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:102)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82)
    at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86)
    at com.xtremelabs.robolectric.res.ResourceLoader.inflateView(ResourceLoader.java:336)
    at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:43)
    at com.xtremelabs.robolectric.shadows.ShadowLayoutInflater.inflate(ShadowLayoutInflater.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:100)
    at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:111)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at com.xtremelabs.robolectric.shadows.ShadowActivity.setContentView(ShadowActivity.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:100)
    at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:111)
    at android.app.Activity.setContentView(Activity.java)
    at br.com.About.onCreate(Sobre.java:22)
    at br.com.TestMyApplication.AboutActivityTest(TestMyApplication.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    ... 21 more
Caused by: java.lang.NullPointerException
    at br.com.getCustomFont(FontsUtil.java:20)
    at br.com.ActionBar.<init>(ActionBar.java:69)
    ... 56 more

My test runs perfectly only when I comment this following view (custom RelativeLayout) from my about.xml layout file:

   <br.com.componente.ActionBar
        android:id="@+id/actionbar"
        style="@style/ActionBar" 
        android:layout_alignParentTop="true"/> 

I can't remove this view from the xml file because I need to test it too. Do anyone knows how to fix it?

Thanks

like image 488
manuenlc Avatar asked Nov 03 '22 11:11

manuenlc


1 Answers

I had a similar issue with a third party library I included in a project (likely related to custom attributes). I solved it by adding a function to inflate the custom view and neuter it in the test. Then you could write a separate test class for the custom widget.

like image 180
colabug Avatar answered Nov 12 '22 16:11

colabug