How to test navigation drawer from jetpack components?

Issue

I have 1 activity, 2 fragments, and a navigation drawer. I use jetpack components, it works well. But when I want to test it as here: from https://github.com/android/architecture-samples (I copied all relative code (Gradle files)) it gives me the following error:

Cannot find a version of 'androidx.test:core' that satisfies the version constraints: 
Dependency path 'Tesst:app:unspecified' --> 'androidx.test:core:1.2.0-beta01'
Constraint path 'Tesst:app:unspecified' --> 'androidx.test:core:{strictly 1.2.0-beta01}' 
because of the following reason: mockDebugRuntimeClasspath uses version 1.2.0-beta01
Dependency path 'Tesst:app:unspecified' --> 'androidx.test.ext:junit:1.1.1' --> 
'androidx.test:core:1.2.0'
Dependency path 'Tesst:app:unspecified' --> 'androidx.test:core-ktx:1.2.0-beta01' --> 
'androidx.test:core:1.2.0-beta01'
Dependency path 'Tesst:app:unspecified' --> 'androidx.test.espresso:espresso-intents:3.2.0- 
beta01' --> 'androidx.test:core:1.2.0-beta01'
Dependency path 'Tesst:app:unspecified' --> 'androidx.fragment:fragment-testing:1.1.0- 
alpha07' --> 'androidx.test:core:1.1.0'

Here is the code I used:

simpleTest.kt

@RunWith(AndroidJUnit4::class)
@LargeTest
class ExampleInstrumentedTest {
private val dataBindingIdlingResource  DataBindingIdlingResource()
@Test
fun useAppContext() {
    // Context of the app under test.
    val appContext  InstrumentationRegistry.getInstrumentation().targetContext
    assertEquals("com.example.tesst", appContext.packageName)
}

@Before
fun registerIdlingResource() {
    IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource)
    IdlingRegistry.getInstance().register(dataBindingIdlingResource)
}

@After
fun unregisterIdlingResource() {
    IdlingRegistry.getInstance().unregister(EspressoIdlingResource.countingIdlingResource)
    IdlingRegistry.getInstance().unregister(dataBindingIdlingResource)
  }
}

MainActivity

class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setupNavigationDrawer()
    setSupportActionBar(toolbar)
    val navController : NavController  findNavController(R.id.nav_host_fragment)
    appBarConfiguration 
        AppBarConfiguration.Builder(R.id.FirstFragment, R.id.SecondFragment)
            .setDrawerLayout(drawerLayout)
            .build()
    setupActionBarWithNavController(navController, appBarConfiguration)
    findViewById<NavigationView>(R.id.nav_view)
        .setupWithNavController(navController)
    fab.setOnClickListener { view ->
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
    }
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.menu_main, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    return when (item.itemId) {
        R.id.action_settings -> true
        else -> super.onOptionsItemSelected(item)
    }
}

override fun onSupportNavigateUp(): Boolean {
    return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}


private fun setupNavigationDrawer() {
    drawerLayout  (findViewById<DrawerLayout>(R.id.drawer_layout))
        .apply {
            setStatusBarBackground(R.color.colorPrimaryDark)
        }
 }
}

FirstFragment

class FirstFragment : Fragment() {

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_first, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    view.findViewById<Button>(R.id.button_first).setOnClickListener {
        val action  FirstFragmentDirections.actionFirstFragmentToSecondFragment("From FirstFragment")
        findNavController().navigate(action)
    }
  }
}

SecondFragment

class SecondFragment : Fragment() {

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_second, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    view.findViewById<Button>(R.id.button_second).setOnClickListener {
        findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
    }
 }
}

drawer_actions.xml

<menu xmlns:android"http://schemas.android.com/apk/res/android">

<item
    android:id"@id/FirstFragment"
    android:title"list_title" />
<item
    android:id"@id/SecondFragment"
    android:title"statistics_title" />
</menu>

Any help appreciated 🙂 Thanks in advance.

Solution

I needed to update dependencies :

 val espressoVersion  "3.2.0" 

from

 val espressoVersion  "3.2.0.beta"

and

val androidXTestCoreVersion  "1.2.0"

from

   val androidXTestCoreVersion  "1.2.0.beta01"

Answered By – Nurseyit Tursunkulov

Leave a Comment