如何从 Jetpack 组件测试导航抽屉?

How to test navigation drawer from jetpack components?

我有 1 个 activity、2 个片段和一个导航抽屉。我使用 Jetpack 组件,效果很好。但是当我想在这里测试它时:来自 https://github.com/android/architecture-samples (我复制了所有相关代码(Gradle 文件))它给了我以下错误:

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'

这是我使用的代码:

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)
  }
}

主要活动

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)
        }
 }
}

第一个片段

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)
    }
  }
}

第二个片段

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>

感谢任何帮助 :) 提前致谢。

我需要更新依赖项:

 val espressoVersion = "3.2.0" 

来自

 val espressoVersion = "3.2.0.beta"

val androidXTestCoreVersion = "1.2.0"

来自

   val androidXTestCoreVersion = "1.2.0.beta01"