我想斷言,我正在測試的Acitivty在執行某些操作時完成。不幸的是,到目前爲止,我只是通過在測試結束時添加一些睡眠來斷言它。有沒有更好的辦法 ?Espresso如何測試活動是否完成?
import android.content.Context;
import android.os.Build;
import android.support.test.rule.ActivityTestRule;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertTrue;
@SuppressWarnings("unchecked")
@RunWith(JUnit4.class)
@LargeTest
public class MyActivityTest {
Context context;
@Rule
public ActivityTestRule<MyActivity> activityRule
= new ActivityTestRule(MyActivity.class, true, false);
@Before
public void setup() {
super.setup();
// ...
}
@Test
public void finishAfterSomethingIsPerformed() throws Exception {
activityRule.launchActivity(MyActivity.createIntent(context));
doSomeTesting();
activityRule.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
fireEventThatResultsInTheActivityToFinishItself();
}
});
Thread.sleep(2000); // this is needed :(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
assertTrue(activityRule.getActivity().isDestroyed());
}
}
}
感謝您的答覆。我想我還不夠清楚。我的問題是我沒有第二項活動或第一項活動。我只有一個活動。因此,只有當我開始虛擬活動時,我才能使用您的解決方案活動只是爲了能夠啓動我想要從那裏測試的Activtiy .. – IHeartAndroid
因此,您有可運行的並且想要在完成後執行某些操作? –
我想確保我的活動在fireEventThatResultsInTheActivityToFinishItself()後關閉。叫做 – IHeartAndroid