相关疑难解决方法(0)

使用Robolectric测试自定义视图

我正在尝试使用Robolectric 2.1.1运行单元测试,我无法让它膨胀自定义布局(例如ViewPagerIndicator类).假设这是我的布局:

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

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="test"
            android:id="@+id/test_test"/>

    <com.viewpagerindicator.CirclePageIndicator
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

考虑一下我的测试类:

@RunWith(RobolectricTestRunner.class)
public class TestRoboActivityTest {
    private TestRoboActivity mActivity;

    @Before
    public void setUp() throws Exception {
        mActivity = Robolectric.buildActivity(TestRoboActivity.class).create().get();
    }

    @After
    public void tearDown() throws Exception {
        mActivity = null;
    }

    @Test
    public void testSanity() throws Exception {
        Assert.assertNotNull(mActivity);
    }
}
Run Code Online (Sandbox Code Playgroud)

执行'mvn clean test'会导致

Tests in error:
testSanity(TestRoboActivityTest): XML file .\res\layout\test.xml line #-1 (sorry, not yet implemented): Error inflating class com.viewpagerindicator.CirclePageIndicator

很酷,所以似乎还不支持自定义视图.在他们的网站上检查示例Robolectric项目,一个解决方案可能是从LayoutInflater扩展布局: …

android robolectric

81
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

robolectric ×1