我们正在尝试测试应用程序的行为,因为它在不同的生命周期状态(活动,暂停,停止,销毁)中进行,然后在从被破坏状态返回的每个状态中进行测试。
我们遇到的问题是,在调用instrumentation.callActivityOnCreate(testActivity, new Bundle())
以在活动被销毁后重新创建活动时会抛出异常。
例外情况如下
04-16 15:40:20.840 8989-8989/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: No activity
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1835)
at android.app.Activity.onCreate(Activity.java:916)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:204)
at com.test.TestActivity.onCreate(TestActivity.java:5)
at android.app.Activity.performCreate(Activity.java:5158)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at com.test.TestActivityInstrumentationTest$3.run(TestActivityInstrumentationTest.java:25)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1684)
at android.os.Handler.handleCallback(Handler.java:747)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:151)
at android.app.ActivityThread.main(ActivityThread.java:5152)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
测试代码在
下面import android.app.Activity;
import android.app.Instrumentation;
import android.os.Bundle;
import android.test.ActivityInstrumentationTestCase2;
import com.test.TestActivity;
public class TestActivityInstrumentationTest extends ActivityInstrumentationTestCase2<TestActivity> {
public TestActivityInstrumentationTest() {
super(TestActivity.class);
}
public void testActivityOnDestroyedAndStarted() {
final TestActivity testActivity = getActivity();
final Instrumentation instrumentation = getInstrumentation();
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
instrumentation.callActivityOnDestroy(testActivity);
}
});
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
instrumentation.callActivityOnCreate(testActivity, new Bundle());
}
});
}
}
测试活动
import android.support.v4.app.FragmentActivity;
public class TestActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
有谁知道我们如何避免这种情况?
由于
墩
答案 0 :(得分:0)
添加setUp()
来实例化您的活动,添加tearDown()
来处理测试事件的完成。在这里,我销毁每个测试的活动(我认为这是你想要做的):
/**
* Instance from Solo : provides methods to call the Android user interface.
*
*/
protected Solo solo;
/**
* the Activity under test.
*/
TestActivity activity;
/*
* (non-Javadoc)
*
* @see android.test.ActivityInstrumentationTestCase2#setUp()
*/
@Override
protected void setUp() throws Exception {
activity = getActivity();
// if you are using robotium :
solo = new Solo(getInstrumentation(), getActivity());
}
@Override
public void tearDown() throws Exception {
// tearDown() is run after a test case has finished.
// finishOpenedActivities() will finish all the activities that have
// been opened during the test execution.
solo.finishOpenedActivities();
super.tearDown();
}
测试您是否在每次测试开始时都获得了一个Activity实例:
public void testActivityOnDestroyedAndStarted() {
// ensure a valid handle to the activity has been returned
assertNotNull(activity);
}
当您想测试onDestroy时,请尝试以下代码:
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
// to get the activity that you are testing
activity = getActivity();
// check that you are really having a context
assertNotNull(activity);
// and here you are recreating the activity
instrumentation.callActivityOnCreate(activity, new Bundle());
}
});