Utils { public static Date getRegisterDateAsDdMmYyyy(String registrationDateString) { SimpleDateFormat registrationDateFormat = new SimpleDateFormat("dd/MM/yyyy"); try { java.util.Date date = registrationDateFormat.parse(registrationDateString); return new Date(date.getTime()); } catch (ParseException e) { return new Date(System.currentTimeMillis()); } }  static String toStringResult(List<String> result); static Map convert(String mapString); static Date getRegisterDateAsDdMmYyyy(String registrationDateString); static Date getRegisterDateByYyyyMmDd(String registrationDateString); static void clearAudioFile(String fileName); static void showMessageByToast(Context context, int ResId, int duration); static void showMessageByToast(Context context, String message, int duration);  }
@Test public void should_get_date_when_get_registration_data_string() throws Exception { String registrationDateString = "23/02/2017"; Date date = new Date(new SimpleDateFormat("dd/MM/yyyy").parse(registrationDateString).getTime()); assertThat("Should return same date", Utils.getRegisterDateAsDdMmYyyy(registrationDateString), is(date)); }
@Test public void should_throw_exception_when_data_string_format_error() throws Exception { String str = "aa"; Date date = new Date(System.currentTimeMillis()); Date actual = Utils.getRegisterDateAsDdMmYyyy(str); assertThat("Should return same day", actual.getDay(), is(date.getDay())); assertThat("Should return same month", actual.getMonth(), is(date.getMonth())); assertThat("Should return same year", actual.getYear(), is(date.getYear())); }