我有一个活动,会从extras中获取一个pojo,就像这样:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
MyPojo pojo = extras.getParcelable("pojo");
// do stuff with my pojo here
}
}
// button that shows a toast message
}
我在使用Robolectric进行测试时遇到了困难,我认为我需要使用ShadowIntents和ShadowActivities的组合,但是文档有点薄弱,而且关于这个特定场景的有用教程非常稀少。
目前,我已经想出了以下解决方案:
@Test
public void assertClickingTagSightingDisplaysToast()
{
myActivity = new MyActivity();
myActivity.onCreate(null);
ShadowActivity shadowMyActivity = shadowOf(myActivity);
shadowMyActivity.setIntent(new Intent().putExtra("pojo", generateAPojo()));
ShadowButton shadowButton = (ShadowButton) shadowOf(shadowMyActivity.findViewById(R.id.myButton));
shadowButton.performClick();
assertThat(ShadowToast.getTextOfLatestToast(), equalTo("Button was clicked!"));
}
当我执行这些测试时,我一直收到一个空指针异常,在我的活动中的getIntent().getExtras()
代码行,我认为我模拟意图活动的过程是不正确的。
有人可以帮忙吗?
谢谢