Change # 4 and # 5 . In Robolectric 3. * they shared the fragment launch functions.
For support fragments, you need to add a dependency to your build.gradle :
testCompile "org.robolectric:shadows-support-v4:3.3.2"
Import: org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;
For fragments of the platform, you do not need this dependency. Import: import static org.robolectric.util.FragmentTestUtil.startFragment;
Both of them use the same name startFragment() .
import static org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment; @RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class) public class YourFragmentTest { @Test public void shouldNotBeNull() throws Exception { YourFragment fragment = YourFragment.newInstance(); startFragment( fragment ); assertNotNull( fragment ); } }
Change # 3 . Robolectric 2.4 has an API for supporting regular fragments . You can use the newInstance() template or use the constructor when building the Fragment .
import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertNotNull; import static org.robolectric.util.FragmentTestUtil.startFragment; @RunWith(RobolectricGradleTestRunner.class) public class YourFragmentTest { @Test public void shouldNotBeNull() throws Exception { YourFragment fragment = new YourFragment(); startFragment( fragment ); assertNotNull( fragment ); } }
Change # 2 . There's a new helper if you use support fragments ( one that supports regular actions / fragments should be in the next release ):
import static org.robolectric.util.FragmentTestUtil.startFragment; @Before public void setUp() throws Exception { fragment = YourFragment.newInstance(); startFragment( fragment ); }
Change If you upgraded to Robolectric 2.0:
public static void startFragment( Fragment fragment ) { FragmentActivity activity = Robolectric.buildActivity( FragmentActivity.class ) .create() .start() .resume() .get(); FragmentManager fragmentManager = activity.getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add( fragment, null ); fragmentTransaction.commit(); }
Original answer
As another commentator suggested, you need to use the fragment manager (instead of calling the lifecycle methods listed above).
@RunWith(MyTestRunner.class) public class YourFragmentTest { @Test public void shouldNotBeNull() throws Exception { YourFragment yourFragment = new YourFragment(); startFragment( yourFragment ); assertNotNull( yourFragment ); }
I am creating a test runner and have a function that runs the fragment for me, so I can use it everywhere.
public class MyTestRunner extends RobolectricTestRunner { public MyTestRunner( Class<?> testClass ) throws InitializationError { super( testClass ); } public static void startFragment( Fragment fragment ) { FragmentManager fragmentManager = new FragmentActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add( fragment, null ); fragmentTransaction.commit(); } }