45

嗨,我有以下代码:

@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());
    }
}

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

4

8 回答 8

29

在 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 打印有关不正确创建活动的警告,这可能可以做得更好......

于 2013-11-27T07:23:48.033 回答
20

将其更新为 3.1.2,因为上面的答案对我不起作用:-

    loginButton.callOnClick();

    Intent startedIntent = shadowOf(activity).getNextStartedActivity();
    ShadowIntent shadowIntent = shadowOf(startedIntent);
    assertEquals(NextActivity.class, shadowIntent.getIntentClass()); 
于 2016-09-24T09:19:52.773 回答
18

使用 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));
    }  
}
于 2011-05-26T22:17:26.603 回答
7

受@MichK 的回答启发,这里是使用buildActivityRobolectric 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)));
}
于 2014-05-05T19:46:46.683 回答
2

詹姆斯内维尔的答案适用于 4.3。但是,我使用了 AndroidX API、Espresso 和 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)
    }
}
于 2019-09-22T20:37:52.880 回答
1

这就是 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));
于 2016-07-20T13:50:18.153 回答
1
@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));

}
于 2016-07-18T20:51:37.813 回答
-5

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

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

private static TheActivitysName instance;

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

instance = this;

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

public static TheActivitysName getInstance() {
    return instance;
}

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

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

于 2011-05-25T08:51:41.583 回答