如何使用 Robolectric 3.0 从服务器测试 ListView 项目

How to test ListView items from server using Robolectric 3.0

我正在使用 Robolectric 来测试我的应用程序。在我的应用程序中,我想检查列表视图是否有该视图中的任何项目。并从列表视图中测试 onClickItem。列表视图项目是服务器动态的。每次 activity 启动时,它都会从服务器请求。但是,Robolectric 没有等待响应,所以列表视图上的适配器仍然为 Null。

@RunWith(CustomRobolectricRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 19)
public class HomeScreenTest {

    private HomeActivity activity;

    @Before
    public void setUp() {
        activity = Robolectric.setupActivity(HomeActivity.class);
        FakeHttp.getFakeHttpLayer().interceptHttpRequests(true);
        FakeHttp.setDefaultHttpResponse(200, "OK");
        FakeHttpLayer fakeHttpLayer = FakeHttp.getFakeHttpLayer();
        assertFalse(fakeHttpLayer.hasPendingResponses());
        assertFalse(fakeHttpLayer.hasRequestInfos());
        assertFalse(fakeHttpLayer.hasResponseRules());
    }

    @Test
    public void selectItemCategoryMenuShouldStartSearchResultActivity() throws Exception {
        Thread.sleep(5000);
        ListView leftDrawer = (ListView) activity.findViewById(R.id.left_drawer);
        assertThat(leftDrawer).isNotNull();
        leftDrawer.performItemClick(leftDrawer.getAdapter().getView(0, null, null), 0, leftDrawer.getAdapter().getItemId(0));

        Intent intent = shadowOf(activity).peekNextStartedActivity();
        ShadowIntent shadowIntent = shadowOf(intent);
        assertEquals(SearchResultActivity.class.getName(), shadowIntent.getClass().getName());
    }
}

这是 Robolectric 测试的错误。

Unexpected HTTP call GET http://example.com/api/app/getWallpaper.php HTTP/1.1

java.lang.NullPointerException
    at com.l23rf.android.stockphoto.HomeScreenTest.selectItemCategoryMenuShouldStartSearchResultActivity(HomeScreenTest.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:235)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

如何放置 Robolectric 代码来等待服务器的响应。所以listview adapter不再为null,可以测试listview了。

您在测试和代码中混用了错误的东西:

  • 初始化方法中不应有任何断言(@Before)
  • UI 根本不应调用任何网络,尤其是在 UI 线程

关于第 1 点,请阅读 junit FAQ. For the point #2 I would advise you to read this article 并使用依赖注入来移除 UI 代码中的网络依赖。

我以为你把初始化放错了地方。 首先你必须设置getFakeHttpLayer().interceptHttpRequests(false)。那么它应该是这样的:

@Before
public void setUp() {

    FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
    FakeHttp.setDefaultHttpResponse(200, "OK");
    FakeHttpLayer fakeHttpLayer = FakeHttp.getFakeHttpLayer();
    activity = Robolectric.setupActivity(HomeActivity.class);
    assertFalse(fakeHttpLayer.hasPendingResponses());
    assertFalse(fakeHttpLayer.hasRequestInfos());
    assertFalse(fakeHttpLayer.hasResponseRules());
}

一旦您完成实施请告诉我。