Navigation View Android Kotlin with Espresso - androidx.test.espresso.NoMatchingViewException

58 Views Asked by At

I am new to Espresso trying to write testcase of NavigationView menu item click.

Below method in HomeActivityClass

accountMenu is the NavigationView predefined class, actionView is the View predefined class

private fun updateNavigationItems() {
        with(binding.accountMenu.menu) {
           
        findItem(R.id.nav_account_legal).actionView?.findViewById<TextView>(R.id.item_title)?.text =
                getString(R.string.legal)
         }
    }

Below is the test class which I have implemented

@ExperimentalTextApi
@RunWith(AndroidJUnit4::class)
class HomeActivityTest2 {

    @get : Rule
    var myActivtyRule = ActivityScenarioRule(HomeActivity::class.java)
    
    @Test
    fun legal(){
        onView(withText("Legal")).perform(click())
    }
}

when I try to run legal method I am getting below exception

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: an instance of android.widget.TextView and view.getText() with or without transformation to match: is "Legal"

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1440, height=3120, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION wanim=0x7f150155
fl=LAYOUT_IN_SCREEN FULLSCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=NO_MOVE_ANIMATION FORCE_DRAW_STATUS_BAR_BACKGROUND FIT_INSETS_CONTROLLED
bhv=DEFAULT
fitSides=}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}

Could you please help me on this. I am stuck at this. Thanks in advance.

0

There are 0 best solutions below