AndroidMocks { public static RecyclerView mockRecyclerView() { final RecyclerView recyclerView = mock(RecyclerView.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { when(recyclerView.getAdapter()).thenReturn((RecyclerView.Adapter) invocation.getArguments()[0]); return null; } }).when(recyclerView).setAdapter(any(RecyclerView.Adapter.class)); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { when(recyclerView.getLayoutManager()).thenReturn((RecyclerView.LayoutManager) invocation.getArguments()[0]); return null; } }).when(recyclerView).setLayoutManager(any(RecyclerView.LayoutManager.class)); return recyclerView; }  static Context mockContext(); static MenuItem mockMenuItem(); static MenuItem mockMenuItem(int itemId); static LayoutInflater mockLayoutInflater(); static Intent mockIntent(); static Bundle mockBundle(); static View mockView(ViewPropertyAnimator animator); static View mockView(); static View mockView(Resources resources); @SuppressLint("NewApi") static ViewPropertyAnimator mockViewPropertyAnimator(); static TextView mockTextView(); static TextView mockTextView(Resources resources); static Button mockButton(); static ImageButton mockImageButton(); static Fragment mockFragment(); static Activity mockActivity(Application application); static Activity mockActivity(); static Application mockApplication(); static FragmentManager mockFragmentManager(); @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint({ "CommitTransaction", "NewApi" }) static FragmentManager mockFragmentManager(FragmentTransaction transaction); static Editable mockEditable(String s); static EditText mockEditText(Editable editable); @Deprecated static EditText mockEditField(Editable editable); static EditText mockEditText(String s); @Deprecated static EditText mockEditField(String s); static Resources mockResources(); static EditText prepare(String s, EditText field); static TextView mockTextView(String s); static AutoCompleteTextView mockAutoCompleteEditField(String s); static SharedPreferences.Editor mockEditor(); static AlertDialog.Builder mockDialogBuilder(); static AlertDialog.Builder mockDialogBuilder(ArgumentCaptor<DialogInterface.OnClickListener> positive, ArgumentCaptor<DialogInterface.OnClickListener> negative); static CheckBox mockCheckBox(); static WebView mockWebView(); static ProgressBar mockProgressBar(); static LinearLayout mockLinearLayout(); static RelativeLayout mockRelativeLayout(); static FrameLayout mockFrameLayout(); static RecyclerView mockRecyclerView(); static CardView mockCardView();  }
@Test public void mockRecyclerView() { RecyclerView view = AndroidMocks.mockRecyclerView(); assertThat(view).isNotNull(); assertThat(view.getAdapter()).isNull(); assertThat(view.getLayoutManager()).isNull(); view.setLayoutManager(mock(RecyclerView.LayoutManager.class)); assertThat(view.getLayoutManager()).isNotNull(); view.setAdapter(mock(RecyclerView.Adapter.class)); assertThat(view.getAdapter()).isNotNull(); }