こんにちは私は次のコードを持っています:
@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を押すと新しいアクティビティが開始されることをテストする方法がわかりません。実際にはそうですが、この事実のための正しいRobolectricユニットテストをどのように書くのですか?
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));
}
}
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));
}
}
_
これは私がやっていることに似ています。 new Activity()
を呼び出してActivity
を作成すると、アクティビティの作成に関するRobolectric印刷警告が不適切に生成されることに注意してください。
上記の答えが私にとってうまくいかなかったので、これを3.1.2に更新します:-
loginButton.callOnClick();
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertEquals(NextActivity.class, shadowIntent.getIntentClass());
@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)));
}
@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));
}
これは、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));
James Nevilleの回答は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)
}
}