Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit #4 &amp; #5</strong>: In <a href="https://github.com/robolectric/robolectric/wiki/2.4-to-3.0-Upgrade-Guide" rel="noreferrer">Robolectric 3.*</a>, they split up the fragment starting functions.</p> <p>For support fragments, you will need to add a <a href="https://bintray.com/bintray/jcenter/org.robolectric%3Ashadows-support-v4/view" rel="noreferrer">dependency</a> to your <code>build.gradle</code>:</p> <pre><code>testCompile "org.robolectric:shadows-support-v4:3.3.2" </code></pre> <p>Import: <a href="http://robolectric.org/javadoc/3.0/org/robolectric/shadows/support/v4/SupportFragmentTestUtil.html" rel="noreferrer"><code>org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;</code></a></p> <p>For platform fragments, you don't need this dependency. Import: <a href="http://robolectric.org/javadoc/3.0/org/robolectric/util/FragmentTestUtil" rel="noreferrer"><code>import static org.robolectric.util.FragmentTestUtil.startFragment; </code></a></p> <p>They both use the same name of <code>startFragment()</code>.</p> <pre><code>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 ); } } </code></pre> <p><strong>Edit #3</strong>: Robolectric 2.4 has an <a href="http://robolectric.org/javadoc/2.4/org/robolectric/util/FragmentTestUtil.html" rel="noreferrer">API for support and regular fragments</a>. You can either use the <code>newInstance()</code> pattern or use the constructor when constructing your <code>Fragment</code>'s.</p> <pre><code>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 ); } } </code></pre> <p><strong>Edit #2</strong>: There's a new helper if you're using support fragments (<a href="https://github.com/robolectric/robolectric/blob/master/src/main/java/org/robolectric/util/FragmentTestUtil.java" rel="noreferrer">one that supports regular activities/fragments should be in the next release</a>):</p> <pre><code>import static org.robolectric.util.FragmentTestUtil.startFragment; @Before public void setUp() throws Exception { fragment = YourFragment.newInstance(); startFragment( fragment ); } </code></pre> <p><strong>Edit</strong>: If you upgraded to Robolectric 2.0:</p> <pre><code>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(); } </code></pre> <p><strong>Original answer</strong></p> <p>As the other commenter suggested, you do need to use the fragment manager (instead of calling the lifecycle methods you listed above).</p> <pre><code>@RunWith(MyTestRunner.class) public class YourFragmentTest { @Test public void shouldNotBeNull() throws Exception { YourFragment yourFragment = new YourFragment(); startFragment( yourFragment ); assertNotNull( yourFragment ); } </code></pre> <p>I create a test runner and have a function that starts up a fragment for me so I can use it everywhere.</p> <pre><code>public class MyTestRunner extends RobolectricTestRunner { public MyTestRunner( Class&lt;?&gt; 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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload