Testing that button starts an Activity with Robolectric

后端 未结 8 841
眼角桃花
眼角桃花 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:44

    Updating this for 3.1.2 as the answers above did not work for me:-

        loginButton.callOnClick();
    
        Intent startedIntent = shadowOf(activity).getNextStartedActivity();
        ShadowIntent shadowIntent = shadowOf(startedIntent);
        assertEquals(NextActivity.class, shadowIntent.getIntentClass()); 
    
    0 讨论(0)
  • 2020-12-24 11:49

    Use Robolectric's StartedMatcher

    @RunWith(Test9Runner.class) 
    public class MainActivityTest  {
        private MainActivity activity;
        private Button pressMeButton;
    
        @Before
        public void setUp() throws Exception 
        {
            activity = new MainActivity();
            activity.onCreate(null);
            pressMeButton = (Button) activity.findViewById(R.id.button1);
        }
    
        @Test
        public void shouldStartNextActivityWhenButtonIsClicked() 
        {
            pressMeButton.performClick();
            assertThat(activity, new StartedMatcher(NextActivity.class));
        }  
    }
    
    0 讨论(0)
  • 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)
        }
    }
    
    0 讨论(0)
  • 2020-12-24 11:50

    This is how does it looks like for the Robolectric 3

            // Click on the image view
        mShareLocationImageView.performClick();
    
        // Check the startActivityForResult for ShareUserLocationActivity has been triggered
        ShadowActivity shadowActivity = Shadows.shadowOf(mChatWindowsActivity);
        Intent intent = shadowActivity.peekNextStartedActivityForResult().intent;
        assertThat(intent).hasComponent(new ComponentName(mChatWindowsActivity, ShareUserLocationActivity.class));
    
    0 讨论(0)
  • 2020-12-24 11:52

    Inspired by @MichK's answer, here is a complete running test using the buildActivity method chain from Robolectric 2.2+:

    @Test
    public void testStartScheduleActivity() {
        HomeScreenActivity homeActivity = Robolectric.buildActivity(HomeScreenActivity.class).create().start().visible().get();
        ShadowActivity shadowHome = Robolectric.shadowOf(homeActivity);
        Button btnLaunchSchedule = (Button) homeActivity.findViewById(R.id.btnLaunchSchedule);
        Robolectric.clickOn(btnLaunchSchedule);
    
        assertThat(shadowHome.peekNextStartedActivityForResult().intent.getComponent(), equalTo(new ComponentName(homeActivity, ScheduleActivity.class)));
    }
    
    0 讨论(0)
  • 2020-12-24 11:54
    @Before
    public void setUp() throws Exception {
        mMainActivity = Robolectric.buildActivity(MainActivity.class)
                .create().start().visible().get();
    
        shadowActivity =Shadows.shadowOf(mMainActivity);
        hourlyButton = (Button) mMainActivity.findViewById(R.id.hourlyButton);
    }
    @Test
    public void hourlyActivityButtonTest() throws Exception {
    
       Thread.sleep(5000);
        hourlyButton.performClick();
        Intent intent = shadowActivity.peekNextStartedActivityForResult().intent;
        assertThat(intent.getComponent()).isEqualTo(new ComponentName(mMainActivity, HourlyForecastActivity.class));
    
    }
    
    0 讨论(0)
提交回复
热议问题