本文介绍了测试该按钮可启动Robolectric活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
@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单元测试?
But I have no idea how to test that pressing pressMeButton started a new Activity. Actually it does, but how to write the correct Robolectric unit test for this fact?
推荐答案
使用Robolectric的StartedMatcher
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));
}
}
这篇关于测试该按钮可启动Robolectric活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!