Android:无法在Robolectric测试中夸大自定义视图

时间:2014-08-11 14:40:47

标签: android unit-testing android-custom-view robolectric

所以这可能是这个问题的重复:Testing custom Views with Robolectric 但是既然那个人没有接受答案,我会用自己的代码示例再次提问。 我使用Robolectric 2.3在一个项目上运行单元测试,该项目使用一个名为" here_now.xml"的文件中定义的自定义视图。为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:orientation="vertical"
           android:weightSum="1">

           <me.ambient.meetup.swiper.SwipeListView
                xmlns:swipe="http://schemas.android.com/apk/res-auto"
                android:id="@+id/nearbyPeopleList"
                android:listSelector="#00000000"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                swipe:swipeFrontView="@+id/user_profile_layout"
                swipe:swipeBackView="@+id/user_profile_bg"
                swipe:swipeDrawableChecked="@drawable/plus"
                swipe:swipeDrawableUnchecked="@drawable/minus"
                swipe:swipeCloseAllItemsWhenMoveList="true"
                swipe:swipeMode="both"/>

    </LinearLayout>

那个&#39; SwipeListView&#39;是一个自定义视图,它具有这些自定义XML属性。现在,这在应用程序上编译并运行正常。但是当运行涉及实例化该视图的父活动的Robolectric测试时,我遇到了通胀错误:

me.ambient.meetup.test.HereNowActivityTest > testEditProfileMenuButton FAILED
    android.view.InflateException: XML file /Users/shayak/Documents/SourceCode/MagnetAndroid/magnet-android/meetup/src/main/res/layout/here_now.xml line #-1 (sorry, not yet implemented): Error inflating class me.ambient.meetup.swiper.SwipeListView
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
        at android.app.Activity.setContentView(Activity.java:1895)
        at me.ambient.meetup.HereNowActivity.init(HereNowActivity.scala:93)
        at me.ambient.meetup.HereNowActivity.onCreate(HereNowActivity.scala:34)
        at android.app.Activity.performCreate(Activity.java:5133)
        at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
        at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
        at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
        at org.robolectric.util.ActivityController.create(ActivityController.java:110)
        at org.robolectric.util.ActivityController.create(ActivityController.java:120)
        at me.ambient.meetup.test.HereNowActivityTest.testEditProfileMenuButton(HereNowActivityTest.scala:25)

        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 android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createView(LayoutInflater.java)
            at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
            at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java)
            at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_rInflate(LayoutInflater.java:758)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java)
            at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at android.view.LayoutInflater.$$robo$$LayoutInflater_1d1f_inflate(LayoutInflater.java:353)
            at android.view.LayoutInflater.inflate(LayoutInflater.java)
            at com.android.internal.policy.impl.PhoneWindow.$$robo$$PhoneWindow_1a87_setContentView(PhoneWindow.java:267)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
            at android.app.Activity.$$robo$$Activity_c57b_setContentView(Activity.java:1895)
            at android.app.Activity.setContentView(Activity.java)
            at me.ambient.meetup.HereNowActivity.init(HereNowActivity.scala:93)
            at me.ambient.meetup.HereNowActivity.onCreate(HereNowActivity.scala:34)
            at android.app.Activity.$$robo$$Activity_c57b_performCreate(Activity.java:5133)
            at android.app.Activity.performCreate(Activity.java)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            ... 6 more

            Caused by:
            java.lang.RuntimeException: You forgot the attributes swipeFrontView or swipeBackView. You can add this attributes or use 'swipelist_frontview' and 'swipelist_backview' identifiers
                at me.ambient.meetup.swiper.SwipeListView.init(SwipeListView.java:211)
                at me.ambient.meetup.swiper.SwipeListView.<init>(SwipeListView.java:159)
                ... 36 more

我还将我的Robolectric测试放在Scala FWIW中:

@Config(manifest = "./src/main/AndroidManifest.xml")
@RunWith(classOf[RobolectricTestRunner])
class HereNowActivityTest extends Asserter with ThreadManipulator {

    @Test
    def testEditProfileMenuButton(): Unit = {
        val activity: HereNowActivity = Robolectric.buildActivity(classOf[HereNowActivity]).create().start().get()
        val editProfileItem = activity.findViewById(R.id.action_edit_profile).asInstanceOf[MenuItem]

        activity.onOptionsItemSelected(editProfileItem)

        val shadowActivity = Robolectric.shadowOf(activity)
        val startedIntent = shadowActivity.getNextStartedActivity()
        val shadowIntent = Robolectric.shadowOf(startedIntent)

        assertEquals(shadowIntent.getComponent().getClassName(), "me.ambient.meetup.EditProfileActivity")
    }
}

有没有人知道为什么我在运行Robolectric测试时无法充气自定义视图,以及无法设置任何自定义属性?

0 个答案:

没有答案