Utils { public static void showMessageByToast(Context context, int ResId, int duration) { Toast toast = Toast.makeText(context, ResId, duration); initToastStyle(context, toast); }  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_show_message_by_toast_when_given_res_id() throws Exception { Context context = mock(Context.class); ViewGroup group = mock(ViewGroup.class); Toast toast = mock(Toast.class); TextView textView = mock(TextView.class); Resources resources = mock(Resources.class); when(Toast.makeText(context, 0, Toast.LENGTH_SHORT)).thenReturn(toast); when(toast.getView()).thenReturn(group); when(group.getChildAt(anyInt())).thenReturn(textView); when(context.getResources()).thenReturn(resources); when(resources.getDimension(anyInt())).thenReturn(0f); Utils.showMessageByToast(context, 0, Toast.LENGTH_SHORT); verify(toast, times(1)).getView(); verify(group, times(1)).getChildAt(anyInt()); verify(context, times(1)).getResources(); verify(resources, times(1)).getDimension(anyInt()); verify(toast, times(1)).show(); verify(textView, times(1)).setTextSize(anyFloat()); }
@Test public void should_show_message_by_toast_when_given_message() throws Exception { Context context = mock(Context.class); ViewGroup group = mock(ViewGroup.class); Toast toast = mock(Toast.class); TextView textView = mock(TextView.class); Resources resources = mock(Resources.class); when(Toast.makeText(context, "", Toast.LENGTH_SHORT)).thenReturn(toast); when(toast.getView()).thenReturn(group); when(group.getChildAt(anyInt())).thenReturn(textView); when(context.getResources()).thenReturn(resources); when(resources.getDimension(anyInt())).thenReturn(0f); Utils.showMessageByToast(context, "", Toast.LENGTH_SHORT); verify(toast, times(1)).getView(); verify(group, times(1)).getChildAt(anyInt()); verify(context, times(1)).getResources(); verify(resources, times(1)).getDimension(anyInt()); verify(toast, times(1)).show(); verify(textView, times(1)).setTextSize(anyFloat()); }