Robolectric测试gridview onclick开始新的活动

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

标签: java android gridview robolectric

我陷入了测试,检查下一个Activity是否已启动但是在点击时使用gridview。这意味着如果单击一个adaper,它将启动新的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。

任何想法都会得到满足。谢谢。

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

以下适用于RoboElectric 3.1.2

loginButton.callOnClick();

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