我目前正在尝试使用 junit 或 mockito 对 recyclerview addonitemclick 列表器进行单元测试。这是我的代码:
private void mypicadapter(TreeMap<Integer, List<Photos>> photosMap) {
List<PhotoListItem> mItems = new ArrayList<>();
for (Integer albumId : photosMap.keySet()) {
ListHeader header = new ListHeader();
header.setAlbumId(albumId);
mItems.add(header);
for (Photos photo : photosMap.get(albumId)) {
mItems.add(photo);
}
pAdapter = new PhotoViewerListAdapter(MainActivity.this, mItems);
mRecyclerView.setAdapter(pAdapter);
// set 5 photos per row if List item type --> header , else fill row with header.
GridLayoutManager layoutManager = new GridLayoutManager(this, 5);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (mRecyclerView.getAdapter().getItemViewType(position) == PhotoListItem.HEADER_TYPE)
// return the number of columns so the group header takes a whole row
return 5;
// normal child item takes up 1 cell
return 1;
}
});
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.addOnItemTouchListener(new PhotoItemClickListener(MainActivity.this,
new PhotoItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (pAdapter.getItemViewType(position) == PhotoListItem.HEADER_TYPE) return;
Photos photo = pAdapter.getItem(position);
Intent intent = new Intent(MainActivity.this, DetailViewActivity.class);
intent.putExtra(PHOTO_DETAILS, photo);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
MainActivity.this,
new Pair<>(view.findViewById(R.id.photoItem),
getString(R.string.transition_name_photo))
);
ActivityCompat.startActivity(MainActivity.this, intent, options.toBundle());
}
}));
}
有没有办法可以进行单元测试:addOnItemTouchListener 或 OnItemClickListener/onitemclick,模拟功能等。我对单元测试很陌生,并且一直在网上查找一些教程并且很困惑。任何用于测试功能的分步教程或任何建议都会有所帮助。此外,此功能中任何其他可能的单元可测试场景都会有所帮助。谢谢!