我是Robolectric的新手,也是我新项目的新手,该项目是用广泛的Robolectric测试编写的。
我进行了./gradlew清洁测试,发现测试随机失败。我怀疑@Before代码有问题,但不确定是什么。
以下是JUnit报告的错误示例:
java.lang.IllegalStateException: Must be called from main thread of process
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1411)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:95)
at org.robolectric.util.Scheduler$ScheduledRunnable.run(Scheduler.java:269)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:179)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:160)
at org.robolectric.util.Scheduler.advanceBy(Scheduler.java:143)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:54)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:266)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:306)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at com.acme.android.util.FragmentRule.before(FragmentRule.java:77)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)
以下是片段规则的代码:
public class FragmentRule<F extends Fragment, A extends NavigationDrawerActivity> extends ExternalResource {
private final Class<A> activityClass;
private A activity;
private F fragment;
private String initialFragmentTag = "transaction";
private FragmentRule(Class<A> activityClass) {
this.activityClass = activityClass;
}
public static <F extends Fragment, A extends NavigationDrawerActivity> FragmentRule<F, A> newInstance(Class<A> a) {
return new FragmentRule<>(a);
}
public static <F extends Fragment, A extends NavigationDrawerActivity> FragmentRule<F, A> forFragment(F fragment, Class<A> a) {
FragmentRule<F, A> rule = new FragmentRule<>(a);
rule.fragment = fragment;
return rule;
}
public static <F extends Fragment> FragmentRule<F, FragmentActivity> newInstance() {
return FragmentRule.newInstance(FragmentActivity.class);
}
public static <F extends Fragment> FragmentRule<F, FragmentActivity> forFragment(F fragment) {
return FragmentRule.forFragment(fragment, FragmentActivity.class);
}
private static synchronized void initActivity(Activity activity, Fragment fragment, String initialFragmentTag) {
activity.getFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, fragment)
.addToBackStack(initialFragmentTag)
.commit();
ShadowLooper.unPauseMainLooper();
}
public F withFragment(F fragment) {
return withFragment(fragment, initialFragmentTag);
}
public F withFragment(F fragment, String tag) {
this.fragment = fragment;
this.initialFragmentTag = tag;
initActivity(activity, fragment, initialFragmentTag);
return fragment;
}
public A getActivity() {
return activity;
}
@Override
protected void before() throws Throwable {
activity = Robolectric.buildActivity(activityClass)
.withIntent(new Intent(ShadowApplication.getInstance().getApplicationContext(), activityClass)
.putExtra("enableProgressBar", false))
.create()
.start()
.visible()
.get();
if (fragment != null) {
initActivity(activity, fragment, initialFragmentTag);
}
}
public <N extends Fragment> N getNextStartedFragment() {
activity.getFragmentManager().executePendingTransactions();
return (N) activity.getFragmentManager().findFragmentById(R.id.frame_container);
}
public F getFragment() {
return fragment;
}
public static class FragmentActivity extends NavigationDrawerActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
enableProgressBar = false;
}
}}
对不起代码墙,不知道从哪里开始! Robolectric的版本是3.0。