I am a newbie to Android and want to do unit testing on Recycler view and its adapter using Mockito.
I have mocked the activity and trying to mock its recycler view and add my unit tests to it. I digged into Google to find a solution for it. But didn't get any references. Final after gathering some of the references I have written my test class like this.
public class PhotoViewActivityTest {
@Mock
Context context;
@InjectMocks
private PhotoViewActivity photoViewActivity;
RecyclerView recyclerView;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
recyclerView = Mockito.mock(RecyclerView.class);
MockitoAnnotations.initMocks(this);
photoViewActivity = mock(PhotoViewActivity.class);
}
@Test
public void checkIfRecyclerViewClicked() {
List<PhotoListItem> items = new ArrayList<>();
Photo photo = Mockitio.mock(Photo.class);
items.add(photo));
PhotoViewerListAdapter adapter = Mockito.mock(PhotoViewerListAdapter);
adapter.setData(items);
recycleView.setAdapter(adapter);
// simulate first item was clicked
recyclerView.getChildAt(0).performClick();
//Check if method was invoked with our object
Mockito.verify(adapter).goToDetailActivity(photo);
}
}
But it is giving null pointer exception. I referred this How do I unit test (with JUnit or mockito) recyclerview item clicks