Added in [API level 3](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/ActivityInstrumentationTestCase2#pubctors) \| [Methods](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#inhmethods)

# ActivityInstrumentationTestCase2

*** ** * ** ***

` public abstract class ActivityInstrumentationTestCase2 `   
` ` ` ` ` 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.ActivityInstrumentationTestCase2\<T extends [android.app.Activity](https://developer.android.com/reference/android/app/Activity)\> |

<br />

*** ** * ** ***

<br />

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

Use [ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule) instead. New tests should be written using the [Android Testing Support Library](https://developer.android.com/tools/testing-support-library).

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 `https://developer.android.com/reference/android/test/UiThreadTest`).
- You can inject custom Intents into your Activity (see `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setActivityIntent(android.content.Intent)`).

This class replaces `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase`, which is deprecated. New tests should be written using this base class.

If you prefer an isolated unit test, see `https://developer.android.com/reference/android/test/ActivityUnitTestCase`.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#ActivityInstrumentationTestCase2(java.lang.Class<T>)(https://developer.android.com/reference/java/lang/Class<T> activityClass) ` Creates an `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2`. |
| ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#ActivityInstrumentationTestCase2(java.lang.String,%20java.lang.Class<T>)(https://developer.android.com/reference/java/lang/String pkg, https://developer.android.com/reference/java/lang/Class<T> activityClass) ` *This constructor is deprecated. use `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#ActivityInstrumentationTestCase2(java.lang.Class<T>)` instead* |

| ### Public methods ||
|---|---|
| ` T` | ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#getActivity()() ` Get the Activity under test, starting it if necessary. |
| ` void` | ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setActivityInitialTouchMode(boolean)(boolean initialTouchMode) ` Call this method before the first call to `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#getActivity()` to set the initial touch mode for the Activity under test. |
| ` void` | ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setActivityIntent(android.content.Intent)(https://developer.android.com/reference/android/content/Intent i) ` Call this method before the first call to `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#getActivity()` to inject a customized Intent into the Activity under test. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#runTest()() ` Runs the current unit test. |
| ` void` | ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setUp()() ` |
| ` void` | ` https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#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

### ActivityInstrumentationTestCase2

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

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

Creates an `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2`.

<br />

| Parameters ||
|---|---|
| `activityClass` | `Class`: The activity to test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml <br /> |

### ActivityInstrumentationTestCase2

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

```
public ActivityInstrumentationTestCase2 (String pkg, 
                Class<T> activityClass)
```

<br />

**This constructor is deprecated.**   

use `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#ActivityInstrumentationTestCase2(java.lang.Class<T>)` instead

Creates an `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2`.

<br />

| Parameters ||
|---|---|
| `pkg` | `String`: ignored - no longer in use. <br /> |
| `activityClass` | `Class`: The activity to test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml <br /> |

## Public methods

### getActivity

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

```
public T getActivity ()
```

Get the Activity under test, starting it if necessary. For each test method invocation, the Activity will not actually be created until the first time this method is called.

If you wish to provide custom setup values to your Activity, you may call `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setActivityIntent(android.content.Intent)` and/or `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setActivityInitialTouchMode(boolean)` before your first call to getActivity(). Calling them after your Activity has started will have no effect.

**NOTE:** Activities under test may not be started from within the UI thread. If your test method is annotated with `https://developer.android.com/reference/android/test/UiThreadTest`, then your Activity will be started automatically just before your test method is run. You still call this method in order to get the Activity under test.

<br />

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

### setActivityInitialTouchMode

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

```
public void setActivityInitialTouchMode (boolean initialTouchMode)
```

Call this method before the first call to `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#getActivity()` to set the initial touch mode for the Activity under test.

If you do not call this, the touch mode will be false. If you call this after your Activity has been started, it will have no effect.

**NOTE:** Activities under test may not be started from within the UI thread. If your test method is annotated with `https://developer.android.com/reference/android/test/UiThreadTest`, then you must call `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setActivityInitialTouchMode(boolean)` from `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setUp()`.

<br />

| Parameters ||
|---|---|
| `initialTouchMode` | `boolean`: true if the Activity should be placed into "touch mode" when started <br /> |

### setActivityIntent

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

```
public void setActivityIntent (Intent i)
```

Call this method before the first call to `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#getActivity()` to inject a customized Intent into the Activity under test.

If you do not call this, the default intent will be provided. If you call this after your Activity has been started, it will have no effect.

**NOTE:** Activities under test may not be started from within the UI thread. If your test method is annotated with `https://developer.android.com/reference/android/test/UiThreadTest`, then you must call `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setActivityIntent(android.content.Intent)` from `https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2#setUp()`.

The default Intent (if this method is not called) is: action = `https://developer.android.com/reference/android/content/Intent#ACTION_MAIN` flags = `https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NEW_TASK` All other fields are null or empty.

<br />

| Parameters ||
|---|---|
| `i` | `Intent`: The Intent to start the Activity with, or null to reset to the default Intent. <br /> |

## Protected methods

### runTest

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

```
protected void runTest ()
```

Runs the current unit test. If the unit test is annotated with `https://developer.android.com/reference/android/test/UiThreadTest`, force the Activity to be created before switching to the UI thread.

<br />

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

### setUp

Added in [API level 3](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` |   |

### tearDown

Added in [API level 3](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` |   |