Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/test/ActivityUnitTestCase#pubctors) \| [Methods](https://developer.android.com/reference/android/test/ActivityUnitTestCase#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/test/ActivityUnitTestCase#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/test/ActivityUnitTestCase#inhmethods)

# ActivityUnitTestCase

*** ** * ** ***

` public abstract class ActivityUnitTestCase `   
` ` ` ` ` extends https://developer.android.com/reference/android/test/ActivityTestCase ` ` `

|---|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||||
| ↳ | [android.test.InstrumentationTestCase](https://developer.android.com/reference/android/test/InstrumentationTestCase) |||
|   | ↳ | [android.test.ActivityTestCase](https://developer.android.com/reference/android/test/ActivityTestCase) ||
|   |   | ↳ | android.test.ActivityUnitTestCase\<T extends [android.app.Activity](https://developer.android.com/reference/android/app/Activity)\> |

<br />

*** ** * ** ***

<br />

**This class was deprecated in API level 24.**   

Write [Local Unit Tests](https://developer.android.com/training/testing/unit-testing/local-unit-tests) instead.

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 `https://developer.android.com/reference/android/test/ActivityUnitTestCase#setUp()` and `https://developer.android.com/reference/android/test/ActivityUnitTestCase#tearDown()`.

If you prefer a functional test, see `https://developer.android.com/reference/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:

- `https://developer.android.com/reference/android/app/Activity#createPendingResult(int,%20android.content.Intent,%20int)`
- `https://developer.android.com/reference/android/app/Activity#startActivityIfNeeded(android.content.Intent,%20int)`
- `https://developer.android.com/reference/android/app/Activity#startActivityFromChild(android.app.Activity,%20android.content.Intent,%20int)`
- `https://developer.android.com/reference/android/app/Activity#startNextMatchingActivity(android.content.Intent)`
- `https://developer.android.com/reference/android/app/Activity#getCallingActivity()`
- `https://developer.android.com/reference/android/app/Activity#getCallingPackage()`
- `https://developer.android.com/reference/android/app/Activity#createPendingResult(int,%20android.content.Intent,%20int)`
- `https://developer.android.com/reference/android/app/Activity#getTaskId()`
- `https://developer.android.com/reference/android/app/Activity#isTaskRoot()`
- `https://developer.android.com/reference/android/app/Activity#moveTaskToBack(boolean)`

The following methods may be called but will not do anything. For test purposes, you can use the methods `https://developer.android.com/reference/android/test/ActivityUnitTestCase#getStartedActivityIntent()` and `https://developer.android.com/reference/android/test/ActivityUnitTestCase#getStartedActivityRequest()` to inspect the parameters that they were called with.

- `https://developer.android.com/reference/android/app/Activity#startActivity(android.content.Intent)`
- `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)`

The following methods may be called but will not do anything. For test purposes, you can use the methods `https://developer.android.com/reference/android/test/ActivityUnitTestCase#isFinishCalled()` and `https://developer.android.com/reference/android/test/ActivityUnitTestCase#getFinishedActivityRequest()` to inspect the parameters that they were called with.

- `https://developer.android.com/reference/android/app/Activity#finish()`
- `https://developer.android.com/reference/android/app/Activity#finishFromChild(android.app.Activity)`
- `https://developer.android.com/reference/android/app/Activity#finishActivity(int)`

<br />

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#ActivityUnitTestCase(java.lang.Class<T>)(https://developer.android.com/reference/java/lang/Class<T> activityClass) ` |

| ### Public methods ||
|---|---|
| ` T` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#getActivity()() ` <br /> |
| ` int` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#getFinishedActivityRequest()() ` This method will return the request code if the Activity under test called `https://developer.android.com/reference/android/app/Activity#finishActivity(int)`. |
| ` int` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#getRequestedOrientation()() ` This method will return the value if your Activity under test calls `https://developer.android.com/reference/android/app/Activity#setRequestedOrientation(int)`. |
| ` https://developer.android.com/reference/android/content/Intent` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#getStartedActivityIntent()() ` This method will return the launch intent if your Activity under test calls `https://developer.android.com/reference/android/app/Activity#startActivity(android.content.Intent)` or `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)`. |
| ` int` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#getStartedActivityRequest()() ` This method will return the launch request code if your Activity under test calls `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)`. |
| ` boolean` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#isFinishCalled()() ` This method will notify you if the Activity under test called `https://developer.android.com/reference/android/app/Activity#finish()`, `https://developer.android.com/reference/android/app/Activity#finishFromChild(android.app.Activity)`, or `https://developer.android.com/reference/android/app/Activity#finishActivity(int)`. |
| ` void` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#setActivityContext(android.content.Context)(https://developer.android.com/reference/android/content/Context activityContext) ` If you wish to inject a Mock, Isolated, or otherwise altered context, you can do so here. |
| ` void` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#setApplication(android.app.Application)(https://developer.android.com/reference/android/app/Application application) ` Set the application for use during the test. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#setUp()() ` |
| ` T` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#startActivity(android.content.Intent,%20android.os.Bundle,%20java.lang.Object)(https://developer.android.com/reference/android/content/Intent intent, https://developer.android.com/reference/android/os/Bundle savedInstanceState, https://developer.android.com/reference/java/lang/Object lastNonConfigurationInstance) ` Start the activity under test, in the same way as if it was started by `https://developer.android.com/reference/android/content/Context#startActivity(android.content.Intent)`, providing the arguments it supplied. |
| ` void` | ` https://developer.android.com/reference/android/test/ActivityUnitTestCase#tearDown()() ` Make sure all resources are cleaned up and garbage collected before moving on to the next test. |

### Inherited methods

From class ` https://developer.android.com/reference/android/test/ActivityTestCase ` ` https://developer.android.com/reference/android/app/Activity` ` https://developer.android.com/reference/android/test/ActivityTestCase#getActivity()() ` ` void` ` https://developer.android.com/reference/android/test/ActivityTestCase#scrubClass(java.lang.Class<?>)(https://developer.android.com/reference/java/lang/Class<?> testCaseClass) `

This function is called by various TestCase implementations, at tearDown() time, in order to scrub out any class variables.
` void` ` https://developer.android.com/reference/android/test/ActivityTestCase#setActivity(android.app.Activity)(https://developer.android.com/reference/android/app/Activity testActivity) `

Set the activity under test.
From class ` https://developer.android.com/reference/android/test/InstrumentationTestCase ` ` https://developer.android.com/reference/android/app/Instrumentation` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#getInstrumentation()() `

Inheritors can access the instrumentation using this.
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#injectInsrumentation(android.app.Instrumentation)(https://developer.android.com/reference/android/app/Instrumentation instrumentation) `

*This method was deprecated in API level 15. Incorrect spelling, use `https://developer.android.com/reference/android/test/InstrumentationTestCase#injectInstrumentation(android.app.Instrumentation)` instead.*
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#injectInstrumentation(android.app.Instrumentation)(https://developer.android.com/reference/android/app/Instrumentation instrumentation) `

Injects instrumentation into this test case.
` final <T extends https://developer.android.com/reference/android/app/Activity> T` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#launchActivity(java.lang.String,%20java.lang.Class<T>,%20android.os.Bundle)(https://developer.android.com/reference/java/lang/String pkg, https://developer.android.com/reference/java/lang/Class<T> activityCls, https://developer.android.com/reference/android/os/Bundle extras) `

Utility method for launching an activity.
` final <T extends https://developer.android.com/reference/android/app/Activity> T` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#launchActivityWithIntent(java.lang.String,%20java.lang.Class<T>,%20android.content.Intent)(https://developer.android.com/reference/java/lang/String pkg, https://developer.android.com/reference/java/lang/Class<T> activityCls, https://developer.android.com/reference/android/content/Intent intent) `

Utility method for launching an activity with a specific Intent.
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#runTest()() `

Runs the current unit test.
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#runTestOnUiThread(java.lang.Runnable)(https://developer.android.com/reference/java/lang/Runnable r) `

Helper for running portions of a test on the UI thread.
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#sendKeys(int[])(int... keys) `

Sends a series of key events through instrumentation and waits for idle.
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#sendKeys(java.lang.String)(https://developer.android.com/reference/java/lang/String keysSequence) `

Sends a series of key events through instrumentation and waits for idle.
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#sendRepeatedKeys(int[])(int... keys) `

Sends a series of key events through instrumentation and waits for idle.
` void` ` https://developer.android.com/reference/android/test/InstrumentationTestCase#tearDown()() `

Make sure all resources are cleaned up and garbage collected before moving on to the next test.
From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### ActivityUnitTestCase

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ActivityUnitTestCase (Class<T> activityClass)
```

<br />

<br />

| Parameters ||
|---|---|
| `activityClass` | `Class` <br /> |

## Public methods

### getActivity

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public T getActivity ()
```

<br />

<br />

| Returns ||
|---|---|
| `T` | Returns the activity under test. <br /> |

### getFinishedActivityRequest

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getFinishedActivityRequest ()
```

This method will return the request code if the Activity under test called `https://developer.android.com/reference/android/app/Activity#finishActivity(int)`.

<br />

| Returns ||
|---|---|
| `int` | The request code provided in the start call, or -1 if no finish call was made. <br /> |

### getRequestedOrientation

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getRequestedOrientation ()
```

This method will return the value if your Activity under test calls `https://developer.android.com/reference/android/app/Activity#setRequestedOrientation(int)`.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### getStartedActivityIntent

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Intent getStartedActivityIntent ()
```

This method will return the launch intent if your Activity under test calls `https://developer.android.com/reference/android/app/Activity#startActivity(android.content.Intent)` or `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/Intent` | The Intent provided in the start call, or null if no start call was made. <br /> |

### getStartedActivityRequest

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getStartedActivityRequest ()
```

This method will return the launch request code if your Activity under test calls `https://developer.android.com/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)`.

<br />

| Returns ||
|---|---|
| `int` | The request code provided in the start call, or -1 if no start call was made. <br /> |

### isFinishCalled

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isFinishCalled ()
```

This method will notify you if the Activity under test called `https://developer.android.com/reference/android/app/Activity#finish()`, `https://developer.android.com/reference/android/app/Activity#finishFromChild(android.app.Activity)`, or `https://developer.android.com/reference/android/app/Activity#finishActivity(int)`.

<br />

| Returns ||
|---|---|
| `boolean` | Returns true if one of the listed finish methods was called. <br /> |

### setActivityContext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setActivityContext (Context activityContext)
```

If you wish to inject a Mock, Isolated, or otherwise altered context, you can do so here. You must call this function before calling `https://developer.android.com/reference/android/test/ActivityUnitTestCase#startActivity(android.content.Intent,%20android.os.Bundle,%20java.lang.Object)`. If you wish to obtain a real Context, as a building block, use getInstrumentation().getTargetContext().

<br />

| Parameters ||
|---|---|
| `activityContext` | `Context` <br /> |

### setApplication

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setApplication (Application application)
```

Set the application for use during the test. You must call this function before calling `https://developer.android.com/reference/android/test/ActivityUnitTestCase#startActivity(android.content.Intent,%20android.os.Bundle,%20java.lang.Object)`. If your test does not call this method,

<br />

| Parameters ||
|---|---|
| `application` | `Application`: The Application object that will be injected into the Activity under test. <br /> |

## Protected methods

### setUp

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void setUp ()
```

<br />

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Exception` |   |

### startActivity

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected T startActivity (Intent intent, 
                Bundle savedInstanceState, 
                Object lastNonConfigurationInstance)
```

Start the activity under test, in the same way as if it was started by `https://developer.android.com/reference/android/content/Context#startActivity(android.content.Intent)`, providing the arguments it supplied. When you use this method to start the activity, it will automatically be stopped by `https://developer.android.com/reference/android/test/ActivityUnitTestCase#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.*

<br />

| Parameters ||
|---|---|
| `intent` | `Intent`: The Intent as if supplied to `https://developer.android.com/reference/android/content/Context#startActivity(android.content.Intent)`. <br /> |
| `savedInstanceState` | `Bundle`: The instance state, if you are simulating this part of the life cycle. Typically null. <br /> |
| `lastNonConfigurationInstance` | `Object`: This Object will be available to the Activity if it calls `https://developer.android.com/reference/android/app/Activity#getLastNonConfigurationInstance()`. Typically null. <br /> |

| Returns ||
|---|---|
| `T` | Returns the Activity that was created <br /> |

### tearDown

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void tearDown ()
```

Make sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Exception` |   |