我遇到了制作检查下一个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。
欢迎提供任何想法。谢谢。
mAdapter
没有设置到GridView
上。关于你的测试,有几点需要注意:你应该开始使用mocking library来避免测试中大量的设置代码;你不应该在测试中添加额外的intent,而是应该检查你的被测类是否正确地设置了intent。 - Eugen MartynovgridView.setAdapter(mAdapter);
了。非常感谢! - satryaway