Android Espresso Not able to test login

357 views Asked by At

I am trying to test login screen UI functionality through espresso

How can i mock activity as it requires to activity to be launched before testing.

i am initializing the aws in my activity

override fun onCreate(savedInstanceState: Bundle?) {
 (application as App).getAwsSessionComponent().inject(this)
}

i am getting error in above line how can i skip actual aws session creation while espresso testing

I have tried to write test case as below

@Rule
@JvmField
var mActivityScenarioRule = ActivityScenarioRule(LoginActivity::class.java)


@Test
fun testLoginSuccess() {
    
    onView(ViewMatchers.withId(R.id.edittext_login_email))
        .perform(typeText("[email protected]"))
    onView(ViewMatchers.withId(R.id.edittext_login_password))
        .perform(typeText("pwd123"))
    onView(ViewMatchers.withId(R.id.button_login))
        .perform(click())
}
1

There are 1 answers

1
Oliver Metz On

The approach that worked best for me was to create a special mock buildType where my dependency injection modules created mocked instances of APIs where I didn't want the "real thing".

Create your espresso tests under the src/androidTestMock (the "Mock" tells the UI test to use the mock buildType). In your tests you can retrieve your mocked classes to mock different API responses. Make sure to tell your dependency injection to use singletons for your mocked classes (e.g. @Singleton for Dagger) or your test code might get different instances than your actual code.

Setting up all of this isn't trivial and sometimes fragile. Make sure you get enough benefit out of UI tests.