I use a static utility to create AlertDialog objects, and I create android.app.AlertDialog alone when I discover Robolectric in the classpath. Here is my code:
private static Boolean isRobolectricTest = null; public static boolean isARobolectricUnitTest() { if (isRobolectricTest == null) { try { Class.forName("org.robolectric.Robolectric"); isRobolectricTest = true; } catch (ClassNotFoundException e) { isRobolectricTest = false; } } return isRobolectricTest; } public static DialogInterface createAndShowDialog(Context context, @StringRes int titleResId, String message, @StringRes int negativeTextResId, DialogInterface.OnClickListener negativeClickListener, @StringRes int neutralTextResId, DialogInterface.OnClickListener neutralClickListener, @StringRes int positiveTextResId, DialogInterface.OnClickListener positiveClickListener, boolean cancelable) { if (isARobolectricUnitTest()) { return UiUtils.createDialog(context, titleResId, message, negativeTextResId, negativeClickListener, neutralTextResId, neutralClickListener, positiveTextResId, positiveClickListener, cancelable); } else { return UiUtils.createDialogSupportV7(context, titleResId, message, negativeTextResId, negativeClickListener, neutralTextResId, neutralClickListener, positiveTextResId, positiveClickListener, cancelable); } } private static android.app.AlertDialog createDialog(Context context, @StringRes int titleResId, String message, @StringRes int negativeTextResId, DialogInterface.OnClickListener negativeClickListener, @StringRes int neutralTextResId, DialogInterface.OnClickListener neutralClickListener, @StringRes int positiveTextResId, DialogInterface.OnClickListener positiveClickListener, boolean cancelable) { android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context); builder.setTitle(titleResId); builder.setMessage(message); builder.setNegativeButton(negativeTextResId, negativeClickListener); if ((neutralTextResId != -1) && (neutralClickListener != null)) { builder.setNeutralButton(neutralTextResId, neutralClickListener); } builder.setPositiveButton(positiveTextResId, positiveClickListener); builder.setCancelable(cancelable); android.app.AlertDialog alertDialog = builder.create(); alertDialog.show(); return alertDialog; } private static android.support.v7.app.AlertDialog createDialogSupportV7(Context context, @StringRes int titleResId, String message, @StringRes int negativeTextResId, DialogInterface.OnClickListener negativeClickListener, @StringRes int neutralTextResId, DialogInterface.OnClickListener neutralClickListener, @StringRes int positiveTextResId, DialogInterface.OnClickListener positiveClickListener, boolean cancelable) { android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context); builder.setTitle(titleResId); builder.setMessage(message); builder.setNegativeButton(negativeTextResId, negativeClickListener); if ((neutralTextResId != -1) && (neutralClickListener != null)) { builder.setNeutralButton(neutralTextResId, neutralClickListener); } builder.setPositiveButton(positiveTextResId, positiveClickListener); builder.setCancelable(cancelable); android.support.v7.app.AlertDialog alertDialog = builder.create(); alertDialog.show(); return alertDialog; }
It is not perfect, but it does the job, and it easily removes the hack later when the Robolectric problem is fixed.