# InstrumentationTestCase

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

# InstrumentationTestCase

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/test/InstrumentationTestCase "View this page in Kotlin")\|Java

`
public


class
InstrumentationTestCase
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|--------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.test.InstrumentationTestCase |

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known direct subclasses [ActivityTestCase](https://developer.android.com/reference/android/test/ActivityTestCase),[ProviderTestCase](https://developer.android.com/reference/android/test/ProviderTestCase)\<T extends[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider)\>,[SingleLaunchActivityTestCase](https://developer.android.com/reference/android/test/SingleLaunchActivityTestCase)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\>,[SyncBaseInstrumentation](https://developer.android.com/reference/android/test/SyncBaseInstrumentation) |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ActivityTestCase](https://developer.android.com/reference/android/test/ActivityTestCase)                                                                                                              | *This class was deprecated in API level 24. New tests should be written using the[Android Testing Support Library](https://developer.android.com/tools/testing-support-library).*                                                                                                                            | | [ProviderTestCase](https://developer.android.com/reference/android/test/ProviderTestCase)\<T extends[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider)\>       | *This class was deprecated in API level 5. this class extends InstrumentationTestCase but should extend AndroidTestCase. Use ProviderTestCase2, which corrects this problem, instead.*                                                                                                                       | | [SingleLaunchActivityTestCase](https://developer.android.com/reference/android/test/SingleLaunchActivityTestCase)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\> | *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).*          | | [SyncBaseInstrumentation](https://developer.android.com/reference/android/test/SyncBaseInstrumentation)                                                                                                | *This class was deprecated in API level 24. Use[InstrumentationRegistry](https://developer.android.com/reference/android/support/test/InstrumentationRegistry)instead. New tests should be written using the[Android Testing Support Library](https://developer.android.com/tools/testing-support-library).* | |

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known indirect subclasses [ActivityInstrumentationTestCase](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\>,[ActivityInstrumentationTestCase2](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\>,[ActivityUnitTestCase](https://developer.android.com/reference/android/test/ActivityUnitTestCase)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\> |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ActivityInstrumentationTestCase](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\>   | *This class was deprecated in API level 3. new tests should be written using[ActivityInstrumentationTestCase2](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2), which provides more options for configuring the Activity under test*                         | | [ActivityInstrumentationTestCase2](https://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\> | *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).* | | [ActivityUnitTestCase](https://developer.android.com/reference/android/test/ActivityUnitTestCase)\<T extends[Activity](https://developer.android.com/reference/android/app/Activity)\>                         | *This class was deprecated in API level 24. Write[Local Unit Tests](https://developer.android.com/training/testing/unit-testing/local-unit-tests)instead.*                                                                                                                                          | |

<br />

*** ** * ** ***

<br />

**This class was deprecated in API level 24.**   
Use[InstrumentationRegistry](https://developer.android.com/reference/androidx/test/platform/app/InstrumentationRegistry)instead. New tests should be written using the[AndroidX Test Library](https://developer.android.com/training/testing).

A test case that has access to[Instrumentation](https://developer.android.com/reference/android/app/Instrumentation).

## Summary

|                                                           ### Public constructors                                                            ||
|-------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[InstrumentationTestCase](https://developer.android.com/reference/android/test/InstrumentationTestCase#InstrumentationTestCase())`() ` |

|                                                                                                                                                                                                                                                                                                              ### Public methods                                                                                                                                                                                                                                                                                                              ||
|---------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[Instrumentation](https://developer.android.com/reference/android/app/Instrumentation)         | ` `[getInstrumentation](https://developer.android.com/reference/android/test/InstrumentationTestCase#getInstrumentation())`() ` Inheritors can access the instrumentation using this.                                                                                                                                                                                                                                                                                                                                                     |
| ` void`                                                                                           | ` `[injectInsrumentation](https://developer.android.com/reference/android/test/InstrumentationTestCase#injectInsrumentation(android.app.Instrumentation))`(`[Instrumentation](https://developer.android.com/reference/android/app/Instrumentation)` instrumentation) ` *This method was deprecated in API level 15. Incorrect spelling, use[injectInstrumentation(android.app.Instrumentation)](https://developer.android.com/reference/android/test/InstrumentationTestCase#injectInstrumentation(android.app.Instrumentation))instead.* |
| ` void`                                                                                           | ` `[injectInstrumentation](https://developer.android.com/reference/android/test/InstrumentationTestCase#injectInstrumentation(android.app.Instrumentation))`(`[Instrumentation](https://developer.android.com/reference/android/app/Instrumentation)` instrumentation) ` Injects instrumentation into this test case.                                                                                                                                                                                                                     |
| ` final <T extends `[Activity](https://developer.android.com/reference/android/app/Activity)`> T` | ` `[launchActivity](https://developer.android.com/reference/android/test/InstrumentationTestCase#launchActivity(java.lang.String,%20java.lang.Class<T>,%20android.os.Bundle))`(`[String](https://developer.android.com/reference/java/lang/String)` pkg, `[Class](https://developer.android.com/reference/java/lang/Class)`<T> activityCls, `[Bundle](https://developer.android.com/reference/android/os/Bundle)` extras) ` Utility method for launching an activity.                                                                     |
| ` final <T extends `[Activity](https://developer.android.com/reference/android/app/Activity)`> T` | ` `[launchActivityWithIntent](https://developer.android.com/reference/android/test/InstrumentationTestCase#launchActivityWithIntent(java.lang.String,%20java.lang.Class<T>,%20android.content.Intent))`(`[String](https://developer.android.com/reference/java/lang/String)` pkg, `[Class](https://developer.android.com/reference/java/lang/Class)`<T> activityCls, `[Intent](https://developer.android.com/reference/android/content/Intent)` intent) ` Utility method for launching an activity with a specific Intent.                |
| ` void`                                                                                           | ` `[runTestOnUiThread](https://developer.android.com/reference/android/test/InstrumentationTestCase#runTestOnUiThread(java.lang.Runnable))`(`[Runnable](https://developer.android.com/reference/java/lang/Runnable)` r) ` Helper for running portions of a test on the UI thread.                                                                                                                                                                                                                                                         |
| ` void`                                                                                           | ` `[sendKeys](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`                                                                                           | ` `[sendKeys](https://developer.android.com/reference/android/test/InstrumentationTestCase#sendKeys(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` keysSequence) ` Sends a series of key events through instrumentation and waits for idle.                                                                                                                                                                                                                                                     |
| ` void`                                                                                           | ` `[sendRepeatedKeys](https://developer.android.com/reference/android/test/InstrumentationTestCase#sendRepeatedKeys(int[]))`(int... keys) ` Sends a series of key events through instrumentation and waits for idle.                                                                                                                                                                                                                                                                                                                      |

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

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](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 `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](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`                                                                  | ` `[wait](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`                                                                  | ` `[wait](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

### InstrumentationTestCase

```
public InstrumentationTestCase ()
```

<br />

<br />

## Public methods

### getInstrumentation

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

```
public Instrumentation getInstrumentation ()
```

Inheritors can access the instrumentation using this.

<br />

|                                                    Returns                                                     ||
|----------------------------------------------------------------------------------------|------------------------|
| [Instrumentation](https://developer.android.com/reference/android/app/Instrumentation) | instrumentation <br /> |

### injectInsrumentation

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

```
public void injectInsrumentation (Instrumentation instrumentation)
```

<br />

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

Injects instrumentation into this test case. This method is called by the test runner during test setup.

<br />

|                                         Parameters                                         ||
|-------------------|-------------------------------------------------------------------------|
| `instrumentation` | `Instrumentation`: the instrumentation to use with this instance <br /> |

### injectInstrumentation

Added in[API level 5](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)  

```
public void injectInstrumentation (Instrumentation instrumentation)
```

Injects instrumentation into this test case. This method is called by the test runner during test setup.

<br />

|                                         Parameters                                         ||
|-------------------|-------------------------------------------------------------------------|
| `instrumentation` | `Instrumentation`: the instrumentation to use with this instance <br /> |

### launchActivity

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

```
public final T launchActivity (String pkg, 
                Class<T> activityCls, 
                Bundle extras)
```

Utility method for launching an activity.

The[Intent](https://developer.android.com/reference/android/content/Intent)used to launch the Activity is: action =[Intent.ACTION_MAIN](https://developer.android.com/reference/android/content/Intent#ACTION_MAIN)extras = null, unless a custom bundle is provided here All other fields are null or empty.

**NOTE:** The parameter*pkg*must refer to the package identifier of the package hosting the activity to be launched, which is specified in the AndroidManifest.xml file. This is not necessarily the same as the java package name.

<br />

|                                    Parameters                                    ||
|---------------|-------------------------------------------------------------------|
| `pkg`         | `String`: The package hosting the activity to be launched. <br /> |
| `activityCls` | `Class`: The activity class to launch. <br />                     |
| `extras`      | `Bundle`: Optional extra stuff to pass to the activity. <br />    |

|                      Returns                       ||
|-----|-----------------------------------------------|
| `T` | The activity, or null if non launched. <br /> |

### launchActivityWithIntent

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)  

```
public final T launchActivityWithIntent (String pkg, 
                Class<T> activityCls, 
                Intent intent)
```

Utility method for launching an activity with a specific Intent.

**NOTE:** The parameter*pkg*must refer to the package identifier of the package hosting the activity to be launched, which is specified in the AndroidManifest.xml file. This is not necessarily the same as the java package name.

<br />

|                                    Parameters                                    ||
|---------------|-------------------------------------------------------------------|
| `pkg`         | `String`: The package hosting the activity to be launched. <br /> |
| `activityCls` | `Class`: The activity class to launch. <br />                     |
| `intent`      | `Intent`: The intent to launch with <br />                        |

|                      Returns                       ||
|-----|-----------------------------------------------|
| `T` | The activity, or null if non launched. <br /> |

### runTestOnUiThread

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)  

```
public void runTestOnUiThread (Runnable r)
```

Helper for running portions of a test on the UI thread. Note, in most cases it is simpler to annotate the test method with[UiThreadTest](https://developer.android.com/reference/android/test/UiThreadTest), which will run the entire test method on the UI thread. Use this method if you need to switch in and out of the UI thread to perform your test.

<br />

|                                                                      Parameters                                                                      ||
|-----|-------------------------------------------------------------------------------------------------------------------------------------------------|
| `r` | `Runnable`: runnable containing test code in the[Runnable.run()](https://developer.android.com/reference/java/lang/Runnable#run())method <br /> |

|                                   Throws                                    ||
|--------------------------------------------------------------------------|---|
| [Throwable](https://developer.android.com/reference/java/lang/Throwable) |   |

### sendKeys

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

```
public void sendKeys (int... keys)
```

Sends a series of key events through instrumentation and waits for idle. For instance: sendKeys(KEYCODE_DPAD_LEFT, KEYCODE_DPAD_CENTER).

<br />

|                                   Parameters                                   ||
|--------|------------------------------------------------------------------------|
| `keys` | `int`: The series of key codes to send through instrumentation. <br /> |

### sendKeys

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

```
public void sendKeys (String keysSequence)
```

Sends a series of key events through instrumentation and waits for idle. The sequence of keys is a string containing the key names as specified in KeyEvent, without the KEYCODE_ prefix. For instance: sendKeys("DPAD_LEFT A B C DPAD_CENTER"). Each key can be repeated by using the N\* prefix. For instance, to send two KEYCODE_DPAD_LEFT, use the following: sendKeys("2\*DPAD_LEFT").

<br />

|                       Parameters                       ||
|----------------|----------------------------------------|
| `keysSequence` | `String`: The sequence of keys. <br /> |

### sendRepeatedKeys

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

```
public void sendRepeatedKeys (int... keys)
```

Sends a series of key events through instrumentation and waits for idle. Each key code must be preceded by the number of times the key code must be sent. For instance: sendRepeatedKeys(1, KEYCODE_DPAD_CENTER, 2, KEYCODE_DPAD_LEFT).

<br />

|                                         Parameters                                         ||
|--------|------------------------------------------------------------------------------------|
| `keys` | `int`: The series of key repeats and codes to send through instrumentation. <br /> |

## Protected methods

### runTest

Added in[API level 1](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[UiThreadTest](https://developer.android.com/reference/android/test/UiThreadTest), the test is run on the UI thread.

<br />

|                                   Throws                                    ||
|--------------------------------------------------------------------------|---|
| [Throwable](https://developer.android.com/reference/java/lang/Throwable) |   |

### 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                                             ||
|--------------------------------------------------------------------------|---------------------|
|                                                                          | java.lang.Exception |
| [Exception](https://developer.android.com/reference/java/lang/Exception) |                     |