ActivityUnitTestCase
Class Overview
This class provides isolated testing of a single activity. The activity under test will be created with minimal connection to the system infrastructure, and you can inject mocked or wrappered versions of many of Activity’s dependencies. Most of the work is handled automatically here by setUp() and tearDown().
If you prefer a functional test, see ActivityInstrumentationTestCase.
It must be noted that, as a true unit test, your Activity will not be running in the normal system and will not participate in the normal interactions with other Activities. The following methods should not be called in this configuration – most of them will throw exceptions:
- createPendingResult(int, Intent, int)
- startActivityIfNeeded(Intent, int)
- startActivityFromChild(Activity, Intent, int)
- startNextMatchingActivity(Intent)
- getCallingActivity()
- getCallingPackage()
- createPendingResult(int, Intent, int)
- getTaskId()
- isTaskRoot()
- moveTaskToBack(boolean)
- setPersistent(boolean)
The following methods may be called but will not do anything. For test purposes, you can use the methods getStartedActivityIntent() and getStartedActivityRequest() to inspect the parameters that they were called with.
The following methods may be called but will not do anything. For test purposes, you can use the methods isFinishCalled() and getFinishedActivityRequest() to inspect the parameters that they were called with.
ActivityInstrumentationTestCase2
Class Overview
This class provides functional testing of a single activity. The activity under test will be created using the system infrastructure (by calling InstrumentationTestCase.launchActivity()) and you will then be able to manipulate your Activity directly.
Other options supported by this test case include:
- You can run any test method on the UI thread (see UiThreadTest).
- You can inject custom Intents into your Activity (see setActivityIntent(Intent)).
This class replaces ActivityInstrumentationTestCase, which is deprecated. New tests should be written using this base class.
If you prefer an isolated unit test, see ActivityUnitTestCase.
我把兩者的差異性較大的地方用粗體字標出來。
ActivityUnitTestCase的範例在SDK的Sample code裡頭可以找到,ActivityInstrumentationTestCase2則在android source code裡頭有








