Robolectric测试GridView点击启动新活动

3

我遇到了制作检查下一个Activity是否在点击gridview后启动的测试时卡住了。这意味着如果单击一个适配器,它将启动新的Activity(DetailActivity)。我提供了收集数据列表的gridview适配器。

以下是完整代码:

@Test
public void shouldDisplayDetailActivityWhenAdapterClicked() throws Exception{
    List<ImageNode> nodes = new ArrayList<ImageNode>();
    ImageNode node = new ImageNode();
    node.setId(36597698);
    node.setContributorId("halfpoint");
    node.setFileName("halfpoint150200457");
    node.setFolder("halfpoint1502");
    node.setDescription("halfpoint1502");
    node.setMediaType("halfpoint1502");
    node.setUrlThumb(URLHelper.buildThumbUrl(
            node.getId(),
            node.getContributorId(),
            node.getFolder(),
            node.getFileName(),
            node.getDescription()));
    node.setUrlFullSize(URLHelper.buildFullSizeUrl(node.getUrlThumb()));
    nodes.add(node);
    DetailLikeBoxAdapter mAdapter = new DetailLikeBoxAdapter(activity, nodes);
    GridView gridView = (GridView) activity.findViewById(R.id.likebox_gridview);
    View itemView = mAdapter.getView(0, null, gridView);
    gridView.performItemClick(itemView, 0, mAdapter.getItemId(0));
    Intent startedIntent = shadowOf(activity).getNextStartedActivity();
    startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_KEY, node.getId());
    startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_POS, 0);
    startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_URLFULLSIZES,node.getUrlFullSize() );
    startedIntent.putExtra(CommonConstants.IS_BUILD_CATEGORY, false);// get intent of next activity on stack
    ShadowIntent shadowIntent = shadowOf(startedIntent);            // create shadow intent which starts next activity
    assertEquals(DetailActivity.class.getName(), shadowIntent.getComponent().getClassName()); // compare shadow intent w/ desired next activity
}

错误是java.lang.NullpointerException。
欢迎提供任何想法。谢谢。

你向我们展示了大约30行代码,而你的应用程序将有更多的代码行。你在哪里以及为什么会出现NullpointerException? - nenick
2
我认为在执行点击操作时会发生NPE。因为mAdapter没有设置到GridView上。关于你的测试,有几点需要注意:你应该开始使用mocking library来避免测试中大量的设置代码;你不应该在测试中添加额外的intent,而是应该检查你的被测类是否正确地设置了intent。 - Eugen Martynov
哇!我觉得@EugenMartynov是对的!我忘记写gridView.setAdapter(mAdapter);了。非常感谢! - satryaway
2个回答

0

好的。在这个问题困扰了我一段时间后,我成功地让测试工作了。感谢@Eugen Martynov。

@Test
public void shouldDisplayDetailActivityWhenAdapterClicked() throws Exception{
    List<ImageNode> nodes = new ArrayList<ImageNode>();
    ImageNode node = new ImageNode();
    node.setId(36597698);
    node.setContributorId("halfpoint");
    node.setFileName("halfpoint150200457");
    node.setFolder("halfpoint1502");
    node.setDescription("halfpoint1502");
    node.setMediaType("halfpoint1502");
    node.setUrlThumb(URLHelper.buildThumbUrl(
            node.getId(),
            node.getContributorId(),
            node.getFolder(),
            node.getFileName(),
            node.getDescription()));
    node.setUrlFullSize(URLHelper.buildFullSizeUrl(node.getUrlThumb()));
    nodes.add(node);
    DetailLikeBoxAdapter mAdapter = new DetailLikeBoxAdapter(activity, nodes);
    GridView gridView = (GridView) activity.findViewById(R.id.likebox_gridview);
    gridView.setAdapter(mAdapter);
    View itemView = mAdapter.getView(0, null, gridView);
    gridView.performItemClick(itemView, 0, mAdapter.getItemId(0));
    Intent startedIntent = shadowOf(activity).getNextStartedActivity();
    ShadowIntent shadowIntent = shadowOf(startedIntent);            // create shadow intent which starts next activity
    System.out.println(DetailActivity.class.getName()+" "+shadowIntent.getComponent().getClassName() );
    assertEquals(DetailActivity.class.getName(), shadowIntent.getComponent().getClassName()); // compare shadow intent w/ desired next activity
}

0

以下内容适用于RoboElectric 3.1.2

loginButton.callOnClick();

Intent startedIntent = shadowOf(activity).getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertEquals(NextActivity.class.getName(), shadowIntent.getIntentClass()); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接