测试该按钮以Robolectric启动Activity

问题描述 投票:37回答:7

嗨,我有以下代码:

@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 shouldUpdateResultsWhenButtonIsClicked() throws Exception 
    {
        pressMeButton.performClick();
        ShadowActivity shadowActivity = shadowOf(activity);
        Intent intent = shadowActivity.getResultIntent();
        System.out.print(intent.toString());
    }
}

但我不知道如何测试压力按下的MeButton开始了一个新的活动。实际上确实如此,但是如何为这个事实编写正确的Robolectric单元测试呢?

android android-activity android-intent robolectric
7个回答
19
投票

使用Robolectric的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));
    }  
}

24
投票

在Robolectric 2.1.1中,您可以验证Intent是否以下列方式发射新的Activity

@RunWith(RobolectricTestRunner.class)
public class MyTest {
  private ShadowActivity shadowActivity;
  private MyActivity activity;

  @Before
  public void setup() {
    activity = new MyActivity();
    shadowActivity = Robolectric.shadowOf(activity);        
  }

  @Test
  public shouldStartNewActivityWhenSomething() {
    //Perform activity startup
    //Do some action which starts second activity, for example View::performClick()
    //...
    //Check Intent
    Intent intent = shadowActivity.peekNextStartedActivityForResult().intent;
    assertThat(intent.getStringExtra(MySecondActivity.EXTRA_MESSAGE)).isEqualTo("blebleble");
    assertThat(intent.getComponent()).isEqualTo(new ComponentName(activity, MySecondActivity.class));
  }
}

这与我正在做的类似。请注意,通过调用Activity创建new Activity()将使Robolectric打印关于不正确地创建活动的警告,这可能会做得更好......


13
投票

如上面的答案为3.1.2更新此更新对我不起作用: -

    loginButton.callOnClick();

    Intent startedIntent = shadowOf(activity).getNextStartedActivity();
    ShadowIntent shadowIntent = shadowOf(startedIntent);
    assertEquals(NextActivity.class, shadowIntent.getIntentClass()); 

7
投票

受@ MichK的回答启发,这是使用Robolectric 2.2+的buildActivity方法链进行的完整运行测试:

@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
投票

这就是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
投票
@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));

}

-3
投票

没有在android中使用任何单元测试,我不确定这是否会起作用:

在您开始的活动中,您可以创建一个名为“instance”的静态变量。

private static TheActivitysName instance;

在onCreate活动中,您可以设置实例变量:

instance = this;

然后创建一个静态方法来获取此变量。

public static TheActivitysName getInstance() {
    return instance;
}

在测试中,您可以在TheActivitysName.getInstance()上进行测试。如果为null,则表示尚未启动活动。如果它与null不同,则表示已创建活动。

我不确定在活动有时间创建之前是否要执行要检查的代码。

© www.soinside.com 2019 - 2024. All rights reserved.