WEB开发网
开发学院手机开发Android 开发 Android单元测试 阅读

Android单元测试

 2010-05-31 14:53:00 来源:WEB开发网   
核心提示:2.用ActivityUnitTestCaseJava代码public class ForwardingTest extends ActivityUnitTestCase< Forwarding> { private Intent mStartIntent; private Button mButton;

2.用ActivityUnitTestCase

Java代码

public class ForwardingTest extends ActivityUnitTestCase< Forwarding> { private Intent mStartIntent; private Button mButton; public ForwardingTest() { super(Forwarding.class); } @Override protected void setUp() throws Exception { super.setUp(); // In setUp, you can create any shared test data, or set up mock components to inject // into your Activity. But do not call startActivity() until the actual test methods. mStartIntent = new Intent(Intent.ACTION_MAIN); } /** * The name 'test preconditions' is a convention to signal that if this * test doesn't pass, the test case was not set up properly and it might * explain any and all failures in other tests. This is not guaranteed * to run before other tests, as junit uses reflection to find the tests. */ @MediumTest public void testPreconditions() { startActivity(mStartIntent, null, null); mButton = (Button) getActivity().findViewById(R.id.go); assertNotNull(getActivity()); assertNotNull(mButton); } /** * This test demonstrates examining the way that activity calls startActivity() to launch * other activities. */ @MediumTest public void testSubLaunch() { Forwarding activity = startActivity(mStartIntent, null, null); mButton = (Button) activity.findViewById(R.id.go); // This test confirms that when you click the button, the activity attempts to open // another activity (by calling startActivity) and close itself (by calling finish()). mButton.performClick(); assertNotNull(getStartedActivityIntent()); assertTrue(isFinishCalled()); } /** * This test demonstrates ways to exercise the Activity's life cycle. */ @MediumTest public void testLifeCycleCreate() { Forwarding activity = startActivity(mStartIntent, null, null); // At this point, onCreate() has been called, but nothing else // Complete the startup of the activity getInstrumentation().callActivityOnStart(activity); getInstrumentation().callActivityOnResume(activity); // At this point you could

Tags:Android 单元

编辑录入:coldstar [复制链接] [打 印]
赞助商链接