我尝试了几个堆栈溢出问题来解决此问题。我附上该日志,以供参考,如下所示:
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.gzw.presentation.fragment:id/count_view
在我的Testcase类中,我是这样写的:
@Before
public void setUp() throws Exception {
mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
activityRule.getActivity()
.getSupportFragmentManager().beginTransaction();
}
在另一种测试方法中,我正在检查片段布局视图中的UI元素。
@Test
public void testInitializedViews() {
Espresso.onView(withId(R.id.count_view)).check(matches(isDisplayed()));
}
这里R.id.count_view是textview驻留在片段布局中。如果有人知道如何在片段布局视图中使用espresso。在这里让我知道...
注意:google / stackoverflow链接均无法解决此问题。
您应使用FragmentScenario.launchInContainer()方法,此链接可以帮助您:https://github.com/robolectric/robolectric/issues/5157