It's been days but I still don't understand how to successfully create a unit test on Android espresso.
I have this function that is called when the login_staff_member_btn is called.
private fun showLoginEmail(){
val loginEmailFragment: Fragment = LoginEmailFragment()
(activity as MainActivity).replaceFragment(loginEmailFragment, LoginEmailFragment.TAG)
}
I've stated the @Rule to set the activity of the test.
@RunWith(AndroidJUnit4::class)
class LoginSelectTypeFragmentTest {
@get:Rule var activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)
@Test
fun clickStaffMemberBtn() {
launchFragmentInContainer<LoginSelectTypeFragment>(themeResId = R.style.SplashTheme)
onView(withId(R.id.login_staff_member_btn)).perform(click())
}
And I get this error
Caused by: java.lang.ClassCastException: androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity cannot be cast to .MainActivity
It seems like the activity was not set. Any idea what I did wrong or what should be updated?
Please let me know.
Thank you in advance!