Testing that button starts an Activity with Robolectric

后端 未结 8 840
眼角桃花
眼角桃花 2020-12-24 11:25

Hi I have the following code:

@RunWith(Test9Runner.class)
public class MainActivityTest 
{
    private MainActivity activity;
    private Button pressMeButto         


        
8条回答
  •  囚心锁ツ
    2020-12-24 11:50

    James Neville's answer works on 4.3. However, I used the AndroidX API, Espresso and Kotlin:

    // scenario initialization is done in @Before setUp method, I did it here for brevity
    val scenario = ActivityScenario.launch(MainActivity::class.java)
    
    @Test fun test() {
        onView(withId(R.id.button_id)).perform(click())
    
        scenario.onActivity { activity ->
            val intent = shadowOf(activity).nextStartedActivity
            val shadowIntent = shadowOf(intent)
    
            assertEquals(SearchResultsActivity::class.java, shadowIntent.intentClass)
        }
    }
    

提交回复
热议问题