Issue
I'm trying to test my fragment with Espresso. No matter what view I'm trying to test I get NoMatchingViewException
, I'm 100% sure that the view actually exists on the fragment.
This is my test:
@LargeTest
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class ExploreFragmentTest {
private lateinit var blogRepository: FakeBlogRepository
private lateinit var inAppMessagesRepository: FakeInAppMessageRepository
private lateinit var fakeFarRetailsRepository: FakeFarRetailsRepository
private lateinit var fakeUserProfileRepository: FakeUserProfileRepository
@Before
fun initRepository() {
blogRepository = FakeBlogRepository()
inAppMessagesRepository = FakeInAppMessageRepository()
fakeFarRetailsRepository = FakeFarRetailsRepository()
fakeUserProfileRepository = FakeUserProfileRepository()
ServiceLocator.blogRepository = blogRepository
ServiceLocator.inAppMessagesRepository = inAppMessagesRepository
ServiceLocator.userProfileRepository = fakeUserProfileRepository
}
@Test
fun noRetailsNearby_displaysNoRetailsNearbyLayout() = runBlockingTest {
// GIVEN - A fake repository with far retails
ServiceLocator.retailsRepository = fakeFarRetailsRepository
// WHEN - ExploreFragment launched to display retails
launchFragmentInContainer<ExploreFragment>(null, R.style.AppTheme)
// THEN - No retails layout is displayed on the screen
onView(withId(R.id.clExploreContainer)).check(matches(isDisplayed()))
}
}
This is the layout of the related fragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/clExploreContainer"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/ablExplore"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="@android:color/transparent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/ctlExploreHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:statusBarScrim="@android:color/transparent"
app:toolbarId="@+id/tbExplore">
<ImageView
android:id="@+id/ivHeaderLemon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:scaleType="centerCrop"
android:src="@drawable/bg_main"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/tbExplore"
android:layout_width="match_parent"
android:layout_height="@dimen/collapsed_toolbar_height"
app:layout_collapseMode="pin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/collapsing_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginStart="20dp"
android:minHeight="?attr/actionBarSize">
<ImageView
android:id="@+id/ivLogo"
android:layout_width="@dimen/toolbar_logo_width"
android:layout_height="@dimen/toolbar_logo_height"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="@drawable/myfoody_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/svExplore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clExploreContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/miniInAppMessageLayout"
layout="@layout/mini_in_app_message_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<include
android:id="@+id/tutorialLayout"
layout="@layout/tutorial_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="20dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/miniInAppMessageLayout"
tools:visibility="visible" />
<include
android:id="@+id/nearToMeLayout"
layout="@layout/near_to_me_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tutorialLayout" />
<include
android:id="@+id/favouritesLayout"
layout="@layout/favourites_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/section_explore_margin_top"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nearToMeLayout"
tools:visibility="visible" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/flCompanyMessageFragment"
android:name="com.anagramma.myfoody.fragments.explore.blog.RecipesFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/section_explore_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/favouritesLayout" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clRemoteMessagesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/flCompanyMessageFragment">
<include
android:id="@+id/rcmlExplore"
layout="@layout/remote_config_message_layout_explore"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<include
android:id="@+id/lMeetMyBrandBannerExplore"
layout="@layout/meet_my_brand_banner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/section_explore_margin_top"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/flLastArticles"
android:name="com.anagramma.myfoody.fragments.explore.blog.LastArticlesFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/section_explore_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/clRemoteMessagesContainer"
tools:layout="@layout/fragment_explore_blog_section" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/flLiveSustainable"
android:name="com.anagramma.myfoody.fragments.explore.blog.LiveSustainableFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/section_explore_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/flLastArticles"
tools:layout="@layout/fragment_explore_blog_section" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/flMyFoodyWorld"
android:name="com.anagramma.myfoody.fragments.explore.blog.MyFoodyWorldFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/section_explore_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/flLiveSustainable"
tools:layout="@layout/fragment_explore_blog_section" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fAutoProduction"
android:name="com.anagramma.myfoody.fragments.explore.blog.AutoproductionFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/section_explore_margin_top"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/flMyFoodyWorld"
tools:layout="@layout/fragment_explore_blog_section" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Every time I run the test I get this error: androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.anagramma.myfoody.staging:id/clExploreContainer
, can't understand why. Am I missing something related to espresso framework?
Solution
My problem was that my fragment had a DialogFragment
covering the entire original fragment view during its startup. Since DialogFragment has another lifecycle, my Espresso test wasn't finding any view on the parent fragment (ExploreFragment
). Once removed the DialogFragment
view matching turned out fine in my test.
Answered By - Nicola Gallazzi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.