使用Robolectric进行碎片测试时“活动已被破坏”错误

时间:2015-06-15 04:41:42

标签: android android-fragments robolectric

我正在尝试使用Fragment在Android上进行Robolectric测试,我知道还有其他一些关于FragmentActivity的问题,当它还没有存储在某个变量中时。

但是,我试图让this post工作,你可以看到在这种情况下保留FragmentActivity对象引用,所以不应该有问题。这是基类:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import org.junit.After;
import org.robolectric.Robolectric;
import org.robolectric.util.ActivityController;

public class FragmentTestCase<T extends Fragment> {

private static final String FRAGMENT_TAG = "fragment";

private ActivityController controller;
private FragmentActivity activity;
private T fragment;

public void startFragment(T fragment) {
    this.fragment = fragment;
    controller = Robolectric.buildActivity(FragmentActivity.class);
    activity = (FragmentActivity) controller.create().start().visible().get();
    activity.getSupportFragmentManager()
            .beginTransaction()
            .add(fragment, FRAGMENT_TAG).commit();
}

@After
public void destroyFragment() {
    if (fragment != null) {
        activity.getSupportFragmentManager()
                .beginTransaction()
                .remove(fragment)
                .commit();

        fragment = null;
    }
}

public void pauseAndResumeFragment() {
    controller.pause().resume();
}

public T recreateFragment() {
    activity.recreate();
    fragment = (T) activity.getSupportFragmentManager()
            .findFragmentByTag(FRAGMENT_TAG);

    return fragment;
 }
}

以下是测试的实施:

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.assertj.core.api.Assertions.*;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants=BuildConfig.class, sdk=21)
public class MainActivityFragmentTest extends FragmentTestCase<MainActivityFragment> {

private MainActivityFragment fragment;

@Before
public void setUp() throws Exception {
    fragment = new MainActivityFragment();
}

@Test
public void createsAndDestroysFragment() throws Exception {
    startFragment(fragment);
    assertThat(fragment).isNotNull();
}

@Test
public void pausesAndResumesFragment() throws Exception {
    startFragment(fragment);
    pauseAndResumeFragment();
    assertThat(fragment).isNotNull();
}

@Test
public void recreatesFragment() throws Exception {
    startFragment(fragment);
    fragment = recreateFragment();
    assertThat(fragment).isNotNull();
  }
 }

一旦我运行测试,这就是我得到的错误:

java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1399)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
at com.feyserflabs.musync.FragmentTestCase.destroyFragment(FragmentTestCase.java:36)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:245)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:149)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

似乎在某些时候Activity在进入destroyFragment方法之前被销毁了,但我无法找到原因。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

当您调用activity.recreate()时,实际上会破坏当前活动,并且您保留的引用将变为无效。然后,您尝试使用已销毁活动的FragmentManager,它会崩溃。