Day62 of #100DaysOfCode

#CodeTogether Day 62/100

Hii folks 🙌

Today I will be continuing the same pathway in which we will test navigation Components.

Unit 3: Navigation

Pathway 2: Introduction to the Navigation component


We will start by creating the test directories and then we will create an instrumentation test class called NavigationTests.kt in the androidTest folder.

  • Then we will add the necessary dependencies as follows:
androidTestImplementation ''
androidTestImplementation 'androidx.navigation:navigation-testing:2.3.5'

debugImplementation 'androidx.fragment:fragment-testing:1.3.6'

Now we will sync the project.

  • After that, we will start writing the navigation Component Test. We will specify the test runner.
  • Now we create a test function called navigate_to_words_nav_component() and create a test instance of the navigation controller.
val navController = TestNavHostController(
  • Navigation Components drive the UI using Fragments.
val letterListScenario = launchFragmentInContainer<LetterListFragment>(themeResId =
  • Lastly, we need to explicitly declare which navigation graph we want the nav controller to use for the fragment launched.
letterListScenario.onFragment { fragment ->


Navigation.setViewNavController(fragment.requireView(), navController)
  • Now we will trigger the event that prompts the navigation.
.actionOnItemAtPosition<RecyclerView.ViewHolder>(2, click()))

Today I Learned:

  • How to test Navigation Components.
  • How to avoid repetitive code with the @Before, @BeforeClass, @After, and @AfterClass annotations.

That is all for Day62 ✅

Thanks for reading, See you tomorrow!

If you are reading my #100Days Journey, feel free to drop by ;)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store