For everyone still trying to figure this out I found a solution:
After poking through Roboelectric's source code you can see that these are the relevant methods:
public class SupportFragmentTestUtil {
public static void startVisibleFragment(Fragment fragment) {
buildSupportFragmentManager(FragmentUtilActivity.class)
.beginTransaction().add(1, fragment, null).commit();
}
private static FragmentManager buildSupportFragmentManager(Class < ? extends FragmentActivity > fragmentActivityClass) {
FragmentActivity activity = Robolectric.setupActivity(fragmentActivityClass);
return activity.getSupportFragmentManager();
}
}
public class Robolectric {
public static < T extends Activity > T setupActivity(Class < T > activityClass) {
return buildActivity(activityClass).setup().get();
}
}
So we can do the same using an Intent:
MyFragment myFragment = new MyFragment()
Intent intent = new Intent(
ShadowApplication.getInstance().applicationContext,
MyActivity::class.java
)
intent.putExtra("type", "email")
MyActivity activity = Robolectric.buildActivity(MyActivity.class, intent).setup().get()
activity.getSupportFragmentManager.beginTransaction().add(1, myFragment, null).commit()
It's working for me (even in Kotlin)
Furthermore I will open a Pull Request to add a method allowing to start a fragment with a specific intent to see that it gets added to next versions of Roboelectric.