Start Activity for testing

前端 未结 5 865
青春惊慌失措
青春惊慌失措 2020-12-30 22:12

I \'ve got a Quiz app using Realm db. Every time the user selects an answer she clicks a button and new text for Question appears. Thats it until she reaches the end where

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 22:22

    For Devs using AndroidX for testing, things are a bit changed.

    This is an example UI Test case for testing whether my intended activity opens after clicking on the textview.

    import androidx.lifecycle.Lifecycle
    import androidx.test.core.app.ActivityScenario
    import androidx.test.espresso.Espresso.onView
    import androidx.test.espresso.action.ViewActions
    import androidx.test.espresso.intent.Intents
    import androidx.test.espresso.intent.Intents.intended
    import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
    import androidx.test.espresso.matcher.ViewMatchers.withId
    import com.softway.dhananjay.tournamentapp.tournament.TournamentActivity
    import org.junit.Test
    
    class MainActivityTest {
    
        @Test
        fun tournament_activity_starts_onClick_of_textView() {
    
            Intents.init()
    
            val activityScenario: ActivityScenario =
                ActivityScenario.launch(MainActivity::class.java)
    
    
            activityScenario.moveToState(Lifecycle.State.RESUMED)
    
            onView(withId(R.id.startTextView)).perform(ViewActions.click())
    
            intended(hasComponent(TournamentActivity::class.java.name))
    
            Intents.release()
    
            activityScenario.moveToState(Lifecycle.State.DESTROYED)
    
        }
    }
    

提交回复
热议问题