Java Robolectric testing gridview onclick启动新活动
我一直在做一个测试,检查下一个活动是否已开始,但在单击时使用gridview。这意味着,如果单击一个适配器,它将启动新活动(DetailActivity)。我提供了gridview适配器,它按列表收集数据 以下是完整的代码:Java Robolectric testing gridview onclick启动新活动,java,android,gridview,robolectric,Java,Android,Gridview,Robolectric,我一直在做一个测试,检查下一个活动是否已开始,但在单击时使用gridview。这意味着,如果单击一个适配器,它将启动新活动(DetailActivity)。我提供了gridview适配器,它按列表收集数据 以下是完整的代码: @Test public void shouldDisplayDetailActivityWhenAdapterClicked() throws Exception{ List<ImageNode> nodes = new ArrayList<Im
@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
}
@测试
public void应显示DetailActivityWhenAdapterClicked()引发异常{
列表节点=新的ArrayList();
ImageNode节点=新的ImageNode();
node.setId(36597698);
node.setContributorId(“半点”);
node.setFileName(“halfpoint150200457”);
node.setFolder(“halfpoint1502”);
node.setDescription(“半点1502”);
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=新的DetailLikeBoxAdapter(活动,节点);
GridView GridView=(GridView)activity.findviewbyd(R.id.likebox\u GridView);
View itemView=mAdapter.getView(0,null,gridView);
performItemClick(itemView,0,mAdapter.getItemId(0));
Intent startedIntent=shadowOf(activity).getNextStartedActivity();
startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_KEY,node.getId());
startedIntent.putExtra(CommonConstants.DETAIL\u IMAGE\u POS,0);
startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_URLFULLSIZES,node.getUrlFullSize());
startedIntent.putExtra(CommonConstants.IS_BUILD_CATEGORY,false);//获取堆栈上下一个活动的意图
ShadowIntent ShadowIntent=shadowOf(startedIntent);//创建开始下一个活动的ShadowIntent
assertEquals(DetailActivity.class.getName(),shadowIntent.getComponent().getClassName());//比较shadow intent与所需的下一个活动
}
错误是java.lang.NullpointerException
任何想法都会受到欢迎。谢谢。好的。在经历了一段糟糕的时间后,我成功地完成了测试。归功于@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
}
@测试
public void应显示DetailActivityWhenAdapterClicked()引发异常{
列表节点=新的ArrayList();
ImageNode节点=新的ImageNode();
node.setId(36597698);
node.setContributorId(“半点”);
node.setFileName(“halfpoint150200457”);
node.setFolder(“halfpoint1502”);
node.setDescription(“半点1502”);
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=新的DetailLikeBoxAdapter(活动,节点);
GridView GridView=(GridView)activity.findviewbyd(R.id.likebox\u GridView);
setAdapter(mAdapter);
View itemView=mAdapter.getView(0,null,gridView);
performItemClick(itemView,0,mAdapter.getItemId(0));
Intent startedIntent=shadowOf(activity).getNextStartedActivity();
ShadowIntent ShadowIntent=shadowOf(startedIntent);//创建开始下一个活动的ShadowIntent
System.out.println(DetailActivity.class.getName()+“”+shadowIntent.getComponent().getClassName());
assertEquals(DetailActivity.class.getName(),shadowIntent.getComponent().getClassName());//比较shadow intent与所需的下一个活动
}
机器人电气3.1.2的以下工作
loginButton.callOnClick();
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertEquals(NextActivity.class.getName(), shadowIntent.getIntentClass());
您向我们展示了大约30行,您的应用程序将有更多行。我认为执行单击时会发生NPE。因为
mAdapter
未设置为GridView
。关于您的测试的几点意见-您应该开始模拟库以避免测试中的大量设置代码,您不应该在测试中添加额外的意图,但应该在测试中检查您的被测类是否正确设置了意图哇!我认为@EugenMartynov是对的!我忘了写下gridView.setAdapter(mAdapter)代码>。非常感谢!