【发布时间】:2021-09-03 10:36:46
【问题描述】:
我正在尝试在片段中为 recyclerview 编写测试,但无论我尝试哪种解决方案,似乎总是遇到相同的错误 (androidx.test.espresso.NoMatchingRootException)。
错误
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));
}
}
openChatFragment() 方法
public void openChatFragment(CloseDialogFragmentInterface closeDialogFragmentInterface) {
chatFragment = new ChatListFragment();
Utils.openFragment(this, chatFragment, false, tasksTitle + "Fragment");
}
openFragment() 方法
public static void openFragment(AppCompatActivity appCompatActivity, Fragment fragment, boolean addTobackStack, String tag) {
try {
FragmentManager fm = appCompatActivity.getSupportFragmentManager();
String fragmentName = fragment.getClass().getSimpleName();
fm.popBackStack(fragmentName, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (addTobackStack) {
fragmentTransaction.addToBackStack(fragmentName);
}
fragmentTransaction.replace(R.id.container, fragment, tag);
fragmentTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
我认为这与查找 recyclerview 的 id 有关,只是无法找出正确的方法。 任何帮助表示赞赏。谢谢
【问题讨论】:
-
请提供
openChatFragment()的代码 -
@Zain ,我已经更新了代码
-
调用只有一个参数
null,方法签名有多个参数 -
请检查我添加了 2 个方法,
openChatFragment使用openFragment,我已经为这两种方法提供了代码
标签: android android-fragments android-recyclerview junit android-espresso