I am currently trying to unit test recyclerview addonitemclick listner, with either junit or mockito. here's my code:
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());
}
}));
}
Is there a way I can unit test : addOnItemTouchListener or OnItemClickListener/onitemclick ,mock the functionality etc. I am pretty new to unit testing and been looking up online at a couple of tutorials and pretty confused. Any step by step tutorial for testing functions or any suggestions would help.Also, any other possible unit testable scenarios in this function would be helpful. Thanks!