# AndroidTestCase

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

# AndroidTestCase

*** ** * ** ***

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

`
public


class
AndroidTestCase
`  
`

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


``


`

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

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known direct subclasses [ApplicationTestCase](https://developer.android.com/reference/android/test/ApplicationTestCase)\<T extends[Application](https://developer.android.com/reference/android/app/Application)\>,[LoaderTestCase](https://developer.android.com/reference/android/test/LoaderTestCase),[ProviderTestCase2](https://developer.android.com/reference/android/test/ProviderTestCase2)\<T extends[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider)\>,[ServiceTestCase](https://developer.android.com/reference/android/test/ServiceTestCase)\<T extends[Service](https://developer.android.com/reference/android/app/Service)\> |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ApplicationTestCase](https://developer.android.com/reference/android/test/ApplicationTestCase)\<T extends[Application](https://developer.android.com/reference/android/app/Application)\>         | *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).* | | [LoaderTestCase](https://developer.android.com/reference/android/test/LoaderTestCase)                                                                                                              | A convenience class for testing[Loader](https://developer.android.com/reference/android/content/Loader)s.                                                                                                                                                                                                    | | [ProviderTestCase2](https://developer.android.com/reference/android/test/ProviderTestCase2)\<T extends[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider)\> | This test case class provides a framework for testing a single[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider)and for testing your app code with an isolated content provider.                                                                                     | | [ServiceTestCase](https://developer.android.com/reference/android/test/ServiceTestCase)\<T extends[Service](https://developer.android.com/reference/android/app/Service)\>                         | *This class was deprecated in API level 24. Use[ServiceTestRule](https://developer.android.com/reference/android/support/test/rule/ServiceTestRule)instead. New tests should be written using the[Android Testing Support Library](https://developer.android.com/tools/testing-support-library).*            | |

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

Extend this if you need to access Resources or other things that depend on Activity Context.

## Summary

|                                                                                        ### Fields                                                                                         ||
|-----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| ` protected `[Context](https://developer.android.com/reference/android/content/Context) | [mContext](https://developer.android.com/reference/android/test/AndroidTestCase#mContext) <br /> |

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

|                                                                                                                                                                                                                                                                                                                                                                                                            ### Public methods                                                                                                                                                                                                                                                                                                                                                                                                             ||
|-------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                       | ` `[assertActivityRequiresPermission](https://developer.android.com/reference/android/test/AndroidTestCase#assertActivityRequiresPermission(java.lang.String,%20java.lang.String,%20java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` packageName, `[String](https://developer.android.com/reference/java/lang/String)` className, `[String](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[SecurityException](https://developer.android.com/reference/java/lang/SecurityException)is thrown that mentions the permission in its error message. |
| ` void`                                                                       | ` `[assertReadingContentUriRequiresPermission](https://developer.android.com/reference/android/test/AndroidTestCase#assertReadingContentUriRequiresPermission(android.net.Uri,%20java.lang.String))`(`[Uri](https://developer.android.com/reference/android/net/Uri)` uri, `[String](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[SecurityException](https://developer.android.com/reference/java/lang/SecurityException)is thrown mentioning the particular permission.                                                                                                                                         |
| ` void`                                                                       | ` `[assertWritingContentUriRequiresPermission](https://developer.android.com/reference/android/test/AndroidTestCase#assertWritingContentUriRequiresPermission(android.net.Uri,%20java.lang.String))`(`[Uri](https://developer.android.com/reference/android/net/Uri)` uri, `[String](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[SecurityException](https://developer.android.com/reference/java/lang/SecurityException)is thrown mentioning the particular permission.                                                                                                                                     |
| ` `[Context](https://developer.android.com/reference/android/content/Context) | ` `[getContext](https://developer.android.com/reference/android/test/AndroidTestCase#getContext())`() `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ` `[Context](https://developer.android.com/reference/android/content/Context) | ` `[getTestContext](https://developer.android.com/reference/android/test/AndroidTestCase#getTestContext())`() ` Returns the test context that was set via[setTestContext(android.content.Context)](https://developer.android.com/reference/android/test/AndroidTestCase#setTestContext(android.content.Context)).                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ` void`                                                                       | ` `[setContext](https://developer.android.com/reference/android/test/AndroidTestCase#setContext(android.content.Context))`(`[Context](https://developer.android.com/reference/android/content/Context)` context) `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ` void`                                                                       | ` `[setTestContext](https://developer.android.com/reference/android/test/AndroidTestCase#setTestContext(android.content.Context))`(`[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`                                                                       | ` `[testAndroidTestCaseSetupProperly](https://developer.android.com/reference/android/test/AndroidTestCase#testAndroidTestCaseSetupProperly())`() `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |

|                                                                                                                                                               ### Protected methods                                                                                                                                                               ||
|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[scrubClass](https://developer.android.com/reference/android/test/AndroidTestCase#scrubClass(java.lang.Class<?>))`(`[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` | ` `[setUp](https://developer.android.com/reference/android/test/AndroidTestCase#setUp())`() `                                                                                                                                                                                                                                            |
| ` void` | ` `[tearDown](https://developer.android.com/reference/android/test/AndroidTestCase#tearDown())`() `                                                                                                                                                                                                                                      |

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

## Fields

### mContext

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

```
protected Context mContext
```

<br />

<br />

## Public constructors

### AndroidTestCase

```
public AndroidTestCase ()
```

<br />

<br />

## Public methods

### assertActivityRequiresPermission

Added in[API level 4](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 assertActivityRequiresPermission (String packageName, 
                String className, 
                String permission)
```

Asserts that launching a given activity is protected by a particular permission by attempting to start the activity and validating that a[SecurityException](https://developer.android.com/reference/java/lang/SecurityException)is thrown that mentions the permission in its error message. Note that an instrumentation isn't needed because all we are looking for is a security error and we don't need to wait for the activity to launch and get a handle to the activity.

<br />

|                                 Parameters                                  ||
|---------------|--------------------------------------------------------------|
| `packageName` | `String`: The package name of the activity to launch. <br /> |
| `className`   | `String`: The class of the activity to launch. <br />        |
| `permission`  | `String`: The name of the permission. <br />                 |

### assertReadingContentUriRequiresPermission

Added in[API level 4](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 assertReadingContentUriRequiresPermission (Uri uri, 
                String permission)
```

Asserts that reading from the content uri requires a particular permission by querying the uri and ensuring a[SecurityException](https://developer.android.com/reference/java/lang/SecurityException)is thrown mentioning the particular permission.

<br />

|                                Parameters                                ||
|--------------|------------------------------------------------------------|
| `uri`        | `Uri`: The uri that requires a permission to query. <br /> |
| `permission` | `String`: The permission that should be required. <br />   |

### assertWritingContentUriRequiresPermission

Added in[API level 4](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 assertWritingContentUriRequiresPermission (Uri uri, 
                String permission)
```

Asserts that writing to the content uri requires a particular permission by inserting into the uri and ensuring a[SecurityException](https://developer.android.com/reference/java/lang/SecurityException)is thrown mentioning the particular permission.

<br />

|                                Parameters                                ||
|--------------|------------------------------------------------------------|
| `uri`        | `Uri`: The uri that requires a permission to query. <br /> |
| `permission` | `String`: The permission that should be required. <br />   |

### getContext

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

```
public Context getContext ()
```

<br />

<br />

|                                      Returns                                       ||
|----------------------------------------------------------------------------|--------|
| [Context](https://developer.android.com/reference/android/content/Context) | <br /> |

### getTestContext

Added in[API level 31](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 Context getTestContext ()
```

Returns the test context that was set via[setTestContext(android.content.Context)](https://developer.android.com/reference/android/test/AndroidTestCase#setTestContext(android.content.Context)).

<br />

|                                      Returns                                       ||
|----------------------------------------------------------------------------|--------|
| [Context](https://developer.android.com/reference/android/content/Context) | <br /> |

### setContext

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

```
public void setContext (Context context)
```

<br />

<br />

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

### setTestContext

Added in[API level 31](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 setTestContext (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. Access to the latter is provided by the context set with the[setContext(Context)](https://developer.android.com/reference/android/test/AndroidTestCase#setContext(android.content.Context))method.

<br />

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

### testAndroidTestCaseSetupProperly

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

```
public void testAndroidTestCaseSetupProperly ()
```

<br />

<br />

## Protected methods

### scrubClass

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

```
protected void scrubClass (Class<?> testCaseClass)
```

This function is called by various TestCase implementations, at tearDown() time, in order to scrub out any class variables. This protects against memory leaks in the case where a test case creates a non-static inner class (thus referencing the test case) and gives it to someone else to hold onto.

<br />

|                                     Parameters                                     ||
|-----------------|-------------------------------------------------------------------|
| `testCaseClass` | `Class`: The class of the derived TestCase implementation. <br /> |

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

### setUp

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

```
protected void setUp ()
```

<br />

<br />

|                                   Throws                                    ||
|--------------------------------------------------------------------------|---|
| [Exception](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 ()
```

<br />

<br />

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