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

时间:2015-05-06 07:06:05

标签: android unit-testing testing robolectric robolectric-gradle-plugin

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

@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$1.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$2.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.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适配器不再为null,并且可以测试listview。

2 个答案:

答案 0 :(得分:0)

你在测试和代码中混合了错误的东西:

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

关于第1点,请阅读junit FAQ。对于第2点,我建议您阅读this article并使用依赖注入来删除UI代码中的网络依赖关系。

答案 1 :(得分:0)

我以为你错过了初始化。 首先,您必须设置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());
}

一旦你完成了它,就告诉我。