1

I am new to writing Android Test Cases. I am trying to write a test case for a project which is already developed. It has so many Activities and Fragments and we are using Navigation Graph for navigating the fragments.

I have started writing test cases from the first fragment and the code is as below:

val navController = TestNavHostController(ApplicationProvider.getApplicationContext())

val scenario = launchFragmentInContainer<SplashFragment>()
scenario.onFragment{
    // Get Auth Navigation
    navController.setGraph(R.navigation.auth_navigation)
    Navigation.setViewNavController(it.requireView(), navController)
    it.redirectUser()
}

Now my SplashFragment contains few methods which have checks like

fun getAppName(): String {
    return (activity as BaseActivity).getAppName()
}

Here, the BaseActivity is the activity which extends AppCompatActivity and it is not defined in my AndroidManifest file.

When I execute the test, I am getting an exception like:

androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity cannot be cast to com.xxx.ui.base.BaseActivity

Can anyone please help me with this?

Aanal Shah
  • 1,946
  • 2
  • 20
  • 30
  • by this https://developer.android.com/guide/fragments/test , there problem use Your fragment shouldn't be dependent on a specific parent activity or fragment. – blay Oct 27 '22 at 13:11
  • https://stackoverflow.com/questions/58651075/fragment-unit-testing-launchfragment-throws-classcastexception – blay Oct 27 '22 at 13:20

0 Answers0