Added in API level 1
Deprecated in API level 24

ActivityUnitTestCase


abstract class ActivityUnitTestCase<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:

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.

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 android.app.Activity#finishActivity(int).

open Int

This method will return the value if your Activity under test calls android.app.Activity#setRequestedOrientation.

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 android.app.Activity#startActivityForResult(Intent, int).

open Boolean

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).

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

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

Inherited functions

Public constructors

ActivityUnitTestCase

Added in API level 1
ActivityUnitTestCase(activityClass: Class<T>!)

Public methods

getActivity

Added in API level 1
open fun getActivity(): T

Deprecated: Deprecated in Java.

Return
T Returns the activity under test.

getFinishedActivityRequest

Added in API level 1
open fun getFinishedActivityRequest(): 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

Added in API level 1
open fun getRequestedOrientation(): Int

Deprecated: Deprecated in Java.

This method will return the value if your Activity under test calls android.app.Activity#setRequestedOrientation.

getStartedActivityIntent

Added in API level 1
open fun getStartedActivityIntent(): 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

Added in API level 1
open fun getStartedActivityRequest(): 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

Added in API level 1
open fun isFinishCalled(): 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

Added in API level 1
open fun setActivityContext(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

Added in API level 1
open fun setApplication(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

setUp

Added in API level 1
protected open fun setUp(): Unit

Deprecated: Deprecated in Java.

startActivity

Added in API level 1
protected open fun startActivity(
    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

Added in API level 1
protected open fun tearDown(): Unit

Deprecated: Deprecated in Java.

Exceptions
java.lang.Exception