我正在尝试使用robolectric测试日期选择器行为。
@Test
public void shouldSetStartedOnDate() {
activity.findViewById(R.id.cruise_form_pick_started_on_button).performClick();
DatePickerDialog dialog = (DatePickerDialog) ShadowDatePickerDialog.getLatestDialog();
assertThat(dialog.isShowing(), is(true));
dialog.updateDate(2013, 3, 13);
ShadowDialog shadowDialog = Robolectric.shadowOf(dialog);
shadowDialog.clickOnText("Done");
assertThat(dialog.isShowing(), is(false));
TextView startedOnText = (TextView) activity.findViewById(R.id.cruise_form_started_on);
assertThat(startedOnText.getText().toString(), equalTo("2013-03-13"));
}
不幸的是dialog.updateDate(2013, 3, 13)
引发了java.lang.NullPointerException
java.lang.NullPointerException
at android.app.DatePickerDialog.updateDate(DatePickerDialog.java:142)
at net.lucassus.yachtexpenses.activities.CruiseFormActivityTest.shouldSetStartedOnDate(CruiseFormActivityTest.java:99)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
...
在调试器中,我注意到dialog.mDatePicker
实例变量是null
。问题是我做错了什么?
不幸的是,我找不到如何用robolectric v2.0测试日期选择器对话框的好例子。