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

# ApplicationTestCase

*** ** * ** ***

` public abstract class ApplicationTestCase `   
` ` ` extends https://developer.android.com/reference/android/test/AndroidTestCase ` ` `

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [android.test.AndroidTestCase](https://developer.android.com/reference/android/test/AndroidTestCase) ||
|   | ↳ | android.test.ApplicationTestCase\<T extends [android.app.Application](https://developer.android.com/reference/android/app/Application)\> |

<br />

*** ** * ** ***

<br />

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

This test case provides a framework in which you can test Application classes in a controlled environment. It provides basic support for the lifecycle of a Application, and hooks by which you can inject various dependencies and control the environment in which your Application is tested.

**Lifecycle Support.** Every Application is designed to be accessed within a specific sequence of method calls (see `https://developer.android.com/reference/android/app/Application` for more details). In order to support the lifecycle of a Application, this test case will make the following calls at the following times.

- The test case will not call onCreate() until your test calls `https://developer.android.com/reference/android/test/ApplicationTestCase#createApplication()`. This gives you a chance to set up or adjust any additional framework or test logic before onCreate().
- After your test completes, the test case `https://developer.android.com/reference/android/test/ApplicationTestCase#tearDown()` method is automatically called, and it will stop \& destroy your application by calling its onDestroy() method.

**Dependency Injection.** Every Application has one inherent dependency, the `https://developer.android.com/reference/android/content/Context` in which it runs. This framework allows you to inject a modified, mock, or isolated replacement for this dependencies, and thus perform a true unit test.

If simply run your tests as-is, your Application will be injected with a fully-functional Context. You can create and inject alternative types of Contexts by calling `https://developer.android.com/reference/android/test/AndroidTestCase#setContext(android.content.Context)`. You must do this *before* calling `https://developer.android.com/reference/android/test/ApplicationTestCase#createApplication()`. The test framework provides a number of alternatives for Context, including `https://developer.android.com/reference/android/test/mock/MockContext`, `https://developer.android.com/reference/android/test/RenamingDelegatingContext`, and `https://developer.android.com/reference/android/content/ContextWrapper`.

## Summary

### Inherited fields

From class `https://developer.android.com/reference/android/test/AndroidTestCase` ` protected https://developer.android.com/reference/android/content/Context` `https://developer.android.com/reference/android/test/AndroidTestCase#mContext`

<br />

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

| ### Public methods ||
|---|---|
| ` T` | ` https://developer.android.com/reference/android/test/ApplicationTestCase#getApplication()() ` |
| ` https://developer.android.com/reference/android/content/Context` | ` https://developer.android.com/reference/android/test/ApplicationTestCase#getSystemContext()() ` Return a real (not mocked or instrumented) system Context that can be used when generating Mock or other Context objects for your Application under test. |
| ` final void` | ` https://developer.android.com/reference/android/test/ApplicationTestCase#testApplicationTestCaseSetUpProperly()() ` This test simply confirms that the Application class can be instantiated properly. |

| ### Protected methods ||
|---|---|
| ` final void` | ` https://developer.android.com/reference/android/test/ApplicationTestCase#createApplication()() ` Start the Application under test, in the same way as if it was started by the system. |
| ` void` | ` https://developer.android.com/reference/android/test/ApplicationTestCase#setUp()() ` This will do the work to instantiate the Application under test. |
| ` void` | ` https://developer.android.com/reference/android/test/ApplicationTestCase#tearDown()() ` Shuts down the Application under test. |
| ` final void` | ` https://developer.android.com/reference/android/test/ApplicationTestCase#terminateApplication()() ` This will make the necessary calls to terminate the Application under test (it will call onTerminate(). |

### Inherited methods

From class ` https://developer.android.com/reference/android/test/AndroidTestCase ` ` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#assertActivityRequiresPermission(java.lang.String,%20java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/lang/String className, https://developer.android.com/reference/java/lang/String permission) `

Asserts that launching a given activity is protected by a particular permission by attempting to start the activity and validating that a `https://developer.android.com/reference/java/lang/SecurityException` is thrown that mentions the permission in its error message.
` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#assertReadingContentUriRequiresPermission(android.net.Uri,%20java.lang.String)(https://developer.android.com/reference/android/net/Uri uri, https://developer.android.com/reference/java/lang/String permission) `

Asserts that reading from the content uri requires a particular permission by querying the uri and ensuring a `https://developer.android.com/reference/java/lang/SecurityException` is thrown mentioning the particular permission.
` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#assertWritingContentUriRequiresPermission(android.net.Uri,%20java.lang.String)(https://developer.android.com/reference/android/net/Uri uri, https://developer.android.com/reference/java/lang/String permission) `

Asserts that writing to the content uri requires a particular permission by inserting into the uri and ensuring a `https://developer.android.com/reference/java/lang/SecurityException` is thrown mentioning the particular permission.
` https://developer.android.com/reference/android/content/Context` ` https://developer.android.com/reference/android/test/AndroidTestCase#getContext()() ` ` https://developer.android.com/reference/android/content/Context` ` https://developer.android.com/reference/android/test/AndroidTestCase#getTestContext()() `

Returns the test context that was set via `https://developer.android.com/reference/android/test/AndroidTestCase#setTestContext(android.content.Context)`.
` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#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/AndroidTestCase#setContext(android.content.Context)(https://developer.android.com/reference/android/content/Context context) ` ` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#setTestContext(android.content.Context)(https://developer.android.com/reference/android/content/Context context) `

Test context can be used to access resources from the test's own package as opposed to the resources from the test target package.
` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#setUp()() ` ` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#tearDown()() ` ` void` ` https://developer.android.com/reference/android/test/AndroidTestCase#testAndroidTestCaseSetupProperly()() ` 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

### ApplicationTestCase

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

```
public ApplicationTestCase (Class<T> applicationClass)
```

<br />

<br />

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

## Public methods

### getApplication

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

```
public T getApplication ()
```

<br />

<br />

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

### getSystemContext

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

```
public Context getSystemContext ()
```

Return a real (not mocked or instrumented) system Context that can be used when generating Mock or other Context objects for your Application under test.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/Context` | Returns a reference to a normal Context. <br /> |

### testApplicationTestCaseSetUpProperly

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

```
public final void testApplicationTestCaseSetUpProperly ()
```

This test simply confirms that the Application class can be instantiated properly.

<br />

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

## Protected methods

### createApplication

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

```
protected final void createApplication ()
```

Start the Application under test, in the same way as if it was started by the system. If you use this method to start the Application, it will automatically be stopped by `https://developer.android.com/reference/android/test/ApplicationTestCase#tearDown()`. If you wish to inject a specialized Context for your test, by calling `https://developer.android.com/reference/android/test/AndroidTestCase#setContext(android.content.Context)`, you must do so before calling this method.

<br />

### setUp

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

```
protected void setUp ()
```

This will do the work to instantiate the Application under test. After this, your test code must also start and stop the Application.

<br />

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

### tearDown

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

```
protected void tearDown ()
```

Shuts down the Application under test. Also makes 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 |
| `https://developer.android.com/reference/java/lang/Exception` |   |

### terminateApplication

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

```
protected final void terminateApplication ()
```

This will make the necessary calls to terminate the Application under test (it will call onTerminate(). Ordinarily this will be called automatically (by `https://developer.android.com/reference/android/test/ApplicationTestCase#tearDown()`, but you can call it directly from your test in order to check for proper shutdown behaviors.

<br />