5

I'm trying to test date picker behavior with 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"));
}

Unfortunately dialog.updateDate(2013, 3, 13) raises 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)
    ...

In the debugger I noticed that dialog.mDatePicker instance variable is null. The question is what am I doing wrong? Unfortunately I can't find good examples how to test date picker dialogs with robolectric v2.0.

4

0 回答 0