您好我有以下代码:
@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单元测试呢?
答案 0 :(得分: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打印关于不正确地创建活动的警告,这可能会做得更好......
答案 1 :(得分: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));
}
}
答案 2 :(得分:13)
为3.1.2更新此答案,因为上面的答案对我不起作用: -
loginButton.callOnClick();
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertEquals(NextActivity.class, shadowIntent.getIntentClass());
答案 3 :(得分: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)));
}
答案 4 :(得分: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));
}
答案 5 :(得分: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));
答案 6 :(得分:0)
詹姆斯·内维尔的答案适用于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)
}
}
答案 7 :(得分:-3)
在Android中没有使用过任何单元测试,我不确定这是否可行:
在您开始的活动中,您可以创建一个名为“instance”的静态变量。
private static TheActivitysName instance;
在onCreate活动中,您可以设置实例变量:
instance = this;
然后创建一个静态方法来获取此变量。
public static TheActivitysName getInstance() {
return instance;
}
在测试中,您可以在TheActivitysName.getInstance()上进行测试。 如果为null,则表示尚未启动活动。如果它与null不同,则表示已创建活动。
我不确定在活动有时间创建之前是否会执行要检查的代码。