Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:16:22+00:00 2026-06-13T00:16:22+00:00

I know there is a Robolectric.shadowOf(Fragment) method and a ShadowFragment class, thought they aren’t

  • 0

I know there is a Robolectric.shadowOf(Fragment) method and a ShadowFragment class, thought they aren’t listed on the docs, but I can’t make it work.

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

I’m working with API level 13 (Honeycomb).

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:16:22+00:00Added an answer on June 13, 2026 at 12:16 am

    Edit #4 & #5: In Robolectric 3.*, they split up the fragment starting functions.

    For support fragments, you will need to add a dependency to your build.gradle:

    testCompile "org.robolectric:shadows-supportv4:3.8"
    

    Import: org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;

    For platform fragments, you don’t need this dependency. Import: import static org.robolectric.util.FragmentTestUtil.startFragment;

    They both use the same name of 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 );
        }
    }
    

    Edit #3: Robolectric 2.4 has an API for support and regular fragments. You can either use the newInstance() pattern or use the constructor when constructing your Fragment‘s.

    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 );
        }
    }
    

    Edit #2: There’s a new helper if you’re using support fragments (one that supports regular activities/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 );
    }
    

    Edit: 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 the other commenter suggested, you do need to use the fragment manager (instead of calling the lifecycle methods you listed above).

    @RunWith(MyTestRunner.class)
    public class YourFragmentTest
    {
        @Test
        public void shouldNotBeNull() throws Exception
        {
            YourFragment yourFragment = new YourFragment();
            startFragment( yourFragment );
            assertNotNull( yourFragment );
        }
    

    I create a test runner and have a function that starts up a 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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are already some solutions to this question but they don't seem
I know there are instructions here , but they are not clear to me
I know there are several questions named like this, but they don't seem to
I know there is a lot on this topic but I can't get any
I know there are a lot of similar questions on SF, but I think
I know there is a wiki page about it , but since I'm very
I know there are a lot of questions around on this subject, but I've
I know there is a function called ISDATE to validate DATETIME columns, but it
I know there is one answer out on stackoverflow, but that is related to
I know there are lots of posts similar to this, but after crawling SO,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.