ActivityUnitTestCase
abstract classActivityUnitTestCase<T : Activity!> : ActivityTestCase
kotlin.Any | |||
↳ | android.test.InstrumentationTestCase | ||
↳ | android.test.ActivityTestCase | ||
↳ | android.test.ActivityUnitTestCase |
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 android.test.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:
android.app.Activity#createPendingResult(int, Intent, int)
android.app.Activity#startActivityIfNeeded(Intent, int)
android.app.Activity#startActivityFromChild(Activity, Intent, int)
android.app.Activity#startNextMatchingActivity(Intent)
android.app.Activity#getCallingActivity()
android.app.Activity#getCallingPackage()
android.app.Activity#createPendingResult(int, Intent, int)
android.app.Activity#getTaskId()
android.app.Activity#isTaskRoot()
android.app.Activity#moveTaskToBack(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.
- android.app.Activity#startActivity(Intent)
android.app.Activity#startActivityForResult(Intent, int)
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.
android.app.Activity#finish()
android.app.Activity#finishFromChild(Activity child)
android.app.Activity#finishActivity(int requestCode)
Summary
Public constructors | |
---|---|
ActivityUnitTestCase(activityClass: Class<T>!) |
Public methods | |
---|---|
open T | |
open Int |
This method will return the request code if the Activity under test called |
open Int |
This method will return the value if your Activity under test calls |
open Intent! |
This method will return the launch intent if your Activity under test calls android. |
open Int |
This method will return the launch request code if your Activity under test calls |
open Boolean |
This method will notify you if the Activity under test called |
open Unit |
setActivityContext(activityContext: Context!) If you wish to inject a Mock, Isolated, or otherwise altered context, you can do so here. |
open Unit |
setApplication(application: Application!) Set the application for use during the test. |
Protected methods | |
---|---|
open Unit |
setUp() |
open T |
startActivity(intent: Intent!, savedInstanceState: Bundle!, lastNonConfigurationInstance: Any!) Start the activity under test, in the same way as if it was started by android. |
open Unit |
tearDown() |
Inherited functions | |
---|---|
Public constructors
Public methods
getActivity
open fungetActivity(): T
Deprecated: Deprecated in Java.
Return | |
---|---|
T |
Returns the activity under test. |
getFinishedActivityRequest
open fungetFinishedActivityRequest(): Int
Deprecated: Deprecated in Java.
This method will return the request code if the Activity under test called android.app.Activity#finishActivity(int)
.
Return | |
---|---|
Int |
The request code provided in the start call, or -1 if no finish call was made. |
getRequestedOrientation
open fungetRequestedOrientation(): Int
Deprecated: Deprecated in Java.
This method will return the value if your Activity under test calls android.app.Activity#setRequestedOrientation
.
getStartedActivityIntent
open fungetStartedActivityIntent(): Intent!
Deprecated: Deprecated in Java.
This method will return the launch intent if your Activity under test calls android.app.Activity#startActivity(Intent) or android.app.Activity#startActivityForResult(Intent, int)
.
Return | |
---|---|
Intent! |
The Intent provided in the start call, or null if no start call was made. |
getStartedActivityRequest
open fungetStartedActivityRequest(): Int
Deprecated: Deprecated in Java.
This method will return the launch request code if your Activity under test calls android.app.Activity#startActivityForResult(Intent, int)
.
Return | |
---|---|
Int |
The request code provided in the start call, or -1 if no start call was made. |
isFinishCalled
open funisFinishCalled(): Boolean
Deprecated: Deprecated in Java.
This method will notify you if the Activity under test called android.app.Activity#finish()
, android.app.Activity#finishFromChild(Activity)
, or android.app.Activity#finishActivity(int)
.
Return | |
---|---|
Boolean |
Returns true if one of the listed finish methods was called. |
setActivityContext
open funsetActivityContext(activityContext: Context!): Unit
Deprecated: Deprecated in Java.
If you wish to inject a Mock, Isolated, or otherwise altered context, you can do so here. You must call this function before calling startActivity
. If you wish to obtain a real Context, as a building block, use getInstrumentation().getTargetContext().
setApplication
open funsetApplication(application: Application!): Unit
Deprecated: Deprecated in Java.
Set the application for use during the test. You must call this function before calling startActivity
. If your test does not call this method,
Parameters | |
---|---|
application |
Application!: The Application object that will be injected into the Activity under test. |
Protected methods
startActivity
protected open funstartActivity(
intent: Intent!,
savedInstanceState: Bundle!,
lastNonConfigurationInstance: Any!
): T
Deprecated: Deprecated in Java.
Start the activity under test, in the same way as if it was started by android.content.Context#startActivity, providing the arguments it supplied. When you use this method to start the activity, it will automatically be stopped by tearDown
.
This method will call onCreate(), but if you wish to further exercise Activity life cycle methods, you must call them yourself from your test case.
Do not call from your setUp() method. You must call this method from each of your test methods.
Parameters | |
---|---|
intent |
Intent!: The Intent as if supplied to android.content.Context#startActivity. |
savedInstanceState |
Bundle!: The instance state, if you are simulating this part of the life cycle. Typically null. |
lastNonConfigurationInstance |
Any!: This Object will be available to the Activity if it calls android.app.Activity#getLastNonConfigurationInstance() . Typically null. |
Return | |
---|---|
T |
Returns the Activity that was created |
tearDown
protected open funtearDown(): Unit
Deprecated: Deprecated in Java.
Exceptions | |
---|---|
java.lang.Exception |