Nir*_*lal 5 android unit-testing mockito android-recyclerview
我是 Android 的新手,想使用 Mockito 对 Recycler 视图及其适配器进行单元测试。
我已经模拟了该活动并试图模拟其回收器视图并将我的单元测试添加到其中。我深入谷歌寻找解决方案。但没有得到任何参考。在收集了一些参考资料后,我写了这样的测试类。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但它给出了空指针异常。我提到了这个How do I unit test (with JUnit or mockito) recyclerview item clicks
| 归档时间: |
|
| 查看次数: |
2799 次 |
| 最近记录: |