3

Working with Robolectric , I'm very new to android. I made a first test class using Activity. It worked nicely. Now I want make a test for fragment.

@RunWith(RobolectricTestRunner.class)
public class LoginFragmentTest {
    private LoginFragment fragment;

    @Before
    public void setup() {
        fragment = new LoginFragment();
        startFragment(fragment);
        assertThat(fragment, notNullValue());
        assertThat(fragment.getActivity(), notNullValue());
    }

    private void startFragment(LoginFragment fragment) {
        FragmentManager fragmentManager = new FragmentActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(fragment, null);
        fragmentTransaction.commit();
    }

    @Test
    public void login() {
        EditText idEditText = (EditText)fragment.getActivity().findViewById(R.id.main_id);
        assertThat(idEditText, notNullValue());
    }
}

This is my first test class for Fragment class. It throws

"java.lang.IllegalStateException: Activity has been destroyed" in startFragment#fragmentTransaction.commit().

Anyone knows how to fix this ?

You can find whole source from https://github.com/msbaek/frame-test

Thanks in advance !!

1
  • hum, don't destroy your activity before commiting a fragment transaction ? Commented Mar 22, 2013 at 10:31

4 Answers 4

4

In my case, specifically, my problem was when creating the activity. I was using

 activity = Robolectric.buildActivity(MyActivity.class).get();

And it should be

 activity = Robolectric.buildActivity(MyActivity.class).create().get();

Hope it helps someone :D

Sign up to request clarification or add additional context in comments.

Comments

3
@RunWith(RobolectricTestRunner.class)
public class LoginFragmentTest {
    private LoginFragment fragment;

    @Before
    public void setup() {
        fragment = new LoginFragment();
        startFragment();
        assertThat(fragment, notNullValue());
        assertThat(fragment.getActivity(), notNullValue());
    }

    private void startFragment() {
        FragmentActivity activity = new FragmentActivity();
        shadowOf(activity).callOnCreate(null);
        shadowOf(activity).callOnStart();
        shadowOf(activity).callOnResume();

        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(fragment, null);
        fragmentTransaction.commit();
    }

    @Test
    public void login() {
        EditText idEditText = (EditText) fragment.getView().findViewById(R.id.main_id);
        assertThat(idEditText, notNullValue());
    }
}

This is working version. Following 3 lines are important(it's from robolectric source - DialogFragmentTest).

        shadowOf(activity).callOnCreate(null);
        shadowOf(activity).callOnStart();
        shadowOf(activity).callOnResume();

Comments

1

The fragments are supposed to be displayed from an Activity. The flow should be:

  • allocate a new fragment object in a FragmentActivity class
  • get the fragment manager to add the newly allocated fragment

In your case you do not have a connection to a real activity. You allocate a FragmentActivity with new FragmentActivity() and try to get the support manager. While this compiles there is no "real" activity able to manage your fragment. Fragments can be added on activities already displayed and here it's not the case.

I recommend reading this page as it explains these things very well: http://developer.android.com/guide/components/fragments.html

3 Comments

Thanks !! I succeeded in connect activity to fragment.
What does "real" activity mean in context of Robolectric?
@백명석 What did you do to connect the fragment to the activity? I am experiencing the similar problem.
1

That happened for me when I used fragmentTransaction.commitAllowingStateLoss(); from sub Fragment whose parent fragment had setRetainInstance(true); I had activity as property what lead to leaking activity on rotation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.