I'm trying to write test for the recyclerview in fragment but always seems to be stucks with same error (androidx.test.espresso.NoMatchingRootException) no matter which solution I try.
Error
androidx.test.espresso.NoMatchingRootException: Matcher 'with decor view is <DecorView@814c399[]>' did not match any of the following roots: [Root{application-window-token=android.view.ViewRootImpl$W@5c6165e, window-token=android.view.ViewRootImpl$W@5c6165e, has-window-focus=false, layout-params-type=1, layout-params-string={(0,0)(fillxfill) sim={adjust=pan} ty=BASE_APPLICATION wanim=0x10302fe
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=2280, has-focus=true, has-focusable=true, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) sim={adjust=pan} ty=BASE_APPLICATION wanim=0x10302fe
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}}]
ListTestFragment.java
public class ListTestFragment {
private ListTestFragment fragment;
@Rule
public ActivityTestRule<BottomNavigationActivity> activityActivityTestRule = new ActivityTestRule<BottomNavigationActivity>(BottomNavigationActivity.class);
@Before
public void setUp() {
activityActivityTestRule.getActivity().getSupportFragmentManager().beginTransaction();
activityActivityTestRule.getActivity().openChatFragment(null);
fragment = activityActivityTestRule.getActivity().chatFragment;
}
@BeforeClass
public static void testClassSetup() {
System.out.println("Getting test class ready");
}
@AfterClass
public static void testClassCleanup() {
System.out.println("Done with tests");
}
@After
public void cleanup() {
System.out.println("Done with unit test!");
}
@Test
public void testSampleRecyclerVisible() {
Espresso.onView((withId(R.id.container)))
.inRoot(RootMatchers.withDecorView(
Matchers.is(activityActivityTestRule.getActivity().getWindow().getDecorView())))
.check(matches(isDisplayed()));
}
@Test
public void testCaseForRecyclerClick() {
Espresso.onView(ViewMatchers.withId(R.id.recyclerview))
.inRoot(RootMatchers.withDecorView(
Matchers.is(activityActivityTestRule.getActivity().getWindow().getDecorView())))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
}
@Test
public void testCaseForRecyclerScroll() {
// Get total item of RecyclerView
RecyclerView recyclerView = activityActivityTestRule.getActivity().findViewById(R.id.recyclerview);
int itemCount = 20;
// Scroll to end of page with position
Espresso.onView(ViewMatchers.withId(R.id.recyclerview))
.inRoot(RootMatchers.withDecorView(
Matchers.is(activityActivityTestRule.getActivity().getWindow().getDecorView())))
.perform(RecyclerViewActions.scrollToPosition(itemCount - 1));
}
}
I believe it's related to finding the id of recyclerview, just can't figure out the correct way to do so. Any help is appreciated. Thanks
from Android Unit Test cases for RecyclerView in Fragment
No comments:
Post a Comment