How can I test fragments using Robolectric?

I know that there is the Robolectric.shadowOf(Fragment) method and the ShadowFragment class, which they believe are not listed in the documents, but I cannot get it to work.

 myFragment = new MyFragment(); myFragment.onCreateView(LayoutInflater.from(activity), (ViewGroup) activity.findViewById(R.id.container), null); myFragment.onAttach(activity); myFragment.onActivityCreated(null); 

I work with API level 13 (Honeycomb).

Thank.

+51
android robolectric
Jul 04 '12 at 17:30
source share
5 answers

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(); } } 
+93
Oct. 15 '12 at 20:14
source share
— -

You guys do it all. Just use FragmentTestUtil.

 FragmentTestUtil.startFragment(yourfragment); 
+33
Jan 22 '14 at 21:28
source share

Support fragments were moved to the module:

shadow-support-v4

(as of July 2015, Robolectric v3.0)

Add the gradle dependency to app / build.gradle:

 testCompile 'org.robolectric:shadows-support-v4:3.0' 

Then import the Robolectric class into your Java class:

 import org.robolectric.shadows.support.v4.SupportFragmentTestUtil; 

Then you can run and use the support fragment-v4 for testing:

 @Test public void minimalFragmentTest() throws Exception { MyFunFragment fragment = new MyFunFragment(); SupportFragmentTestUtil.startVisibleFragment(fragment); assertThat(fragment.getView()).isNotNull(); } 

Literature:

+11
04 Oct '15 at 12:04
source share

I am sure you need to create a FragmentTransaction using the FragmentManager , then it will work.

+2
Jul 20 '12 at 17:12
source share

I just wanted to add this to Robolectric 2.0 even after execution:

 activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get(); fragment.show(activity.getSupportFragmentManager(), null); fragment.getDialog(); //This stills returns null 

It still returns null for me. what I did was add activity.getSupportFragmentManager().executePendingTransaction(); and it worked.

It seems robolectric is not working for some reason. it seems like Luper has stopped or something like that. somehow it worked for me and it looks like this:

 activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get(); fragment.show(activity.getSupportFragmentManager(), null); activity.getSupportFragmentManager().executePendingTransactions(); fragment.getDialog(); 
+2
Sep 05 '13 at 18:55
source share



All Articles