2

我正在使用 Robolectric 测试我的应用程序。在我的应用程序中,我想检查列表视图该视图中是否有任何项目。并从列表视图中测试 onClickItem。列表视图项目是来自服务器的动态。每次启动活动时,它都会从服务器请求。但是,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$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了。

4

2 回答 2

0

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

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

关于第 1 点,请阅读 junit常见问题解答。对于第 2 点,我建议您阅读这篇文章并使用依赖注入来删除 UI 代码中的网络依赖。

于 2015-05-07T06:22:29.387 回答
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());
}

一旦你完成实施它就告诉我。

于 2015-05-08T02:12:53.827 回答