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

# ProviderTestCase2

*** ** * ** ***

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

<br />

*** ** * ** ***

This test case class provides a framework for testing a single `https://developer.android.com/reference/android/content/ContentProvider` and for testing your app code with an isolated content provider. Instead of using the system map of providers that is based on the manifests of other applications, the test case creates its own internal map. It then uses this map to resolve providers given an authority. This allows you to inject test providers and to null out providers that you do not want to use.

This test case also sets up the following mock objects:

- An `https://developer.android.com/reference/android/test/IsolatedContext` that stubs out Context methods that might affect the rest of the running system, while allowing tests to do real file and database work.
- A `https://developer.android.com/reference/android/test/mock/MockContentResolver` that provides the functionality of a regular content resolver, but uses `https://developer.android.com/reference/android/test/IsolatedContext`. It stubs out `https://developer.android.com/reference/android/content/ContentResolver#notifyChange(android.net.Uri,%20android.database.ContentObserver,%20boolean)` to prevent the test from affecting the running system.
- An instance of the provider under test, running in an `https://developer.android.com/reference/android/test/IsolatedContext`.

This framework is set up automatically by the base class' `https://developer.android.com/reference/android/test/ProviderTestCase2#setUp()` method. If you override this method, you must call the super method as the first statement in your override.

In order for their tests to be run, concrete subclasses must provide their own constructor with no arguments. This constructor must call `https://developer.android.com/reference/android/test/ProviderTestCase2#ProviderTestCase2(java.lang.Class<T>,%20java.lang.String)` as its first operation.
For more information on content provider testing, please see [Content Provider Testing](https://developer.android.com/tools/testing/contentprovider_testing).

<br />

## 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/ProviderTestCase2#ProviderTestCase2(java.lang.Class<T>,%20java.lang.String)(https://developer.android.com/reference/java/lang/Class<T> providerClass, https://developer.android.com/reference/java/lang/String providerAuthority) ` Constructor. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/test/mock/MockContentResolver` | ` https://developer.android.com/reference/android/test/ProviderTestCase2#getMockContentResolver()() ` Gets the `https://developer.android.com/reference/android/test/mock/MockContentResolver` created by this class during initialization. |
| ` https://developer.android.com/reference/android/test/IsolatedContext` | ` https://developer.android.com/reference/android/test/ProviderTestCase2#getMockContext()() ` Gets the `https://developer.android.com/reference/android/test/IsolatedContext` created by this class during initialization. |
| ` T` | ` https://developer.android.com/reference/android/test/ProviderTestCase2#getProvider()() ` Returns the content provider created by this class in the `https://developer.android.com/reference/android/test/ProviderTestCase2#setUp()` method. |
| ` static <T extends https://developer.android.com/reference/android/content/ContentProvider> https://developer.android.com/reference/android/content/ContentResolver` | ` https://developer.android.com/reference/android/test/ProviderTestCase2#newResolverWithContentProviderFromSql(android.content.Context,%20java.lang.String,%20java.lang.Class<T>,%20java.lang.String,%20java.lang.String,%20int,%20java.lang.String)(https://developer.android.com/reference/android/content/Context targetContext, https://developer.android.com/reference/java/lang/String filenamePrefix, https://developer.android.com/reference/java/lang/Class<T> providerClass, https://developer.android.com/reference/java/lang/String authority, https://developer.android.com/reference/java/lang/String databaseName, int databaseVersion, https://developer.android.com/reference/java/lang/String sql) ` <br /> Creates a new content provider of the same type as that passed to the test case class, with an authority name set to the authority parameter, and using an SQLite database as the underlying data source. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/test/ProviderTestCase2#setUp()() ` Sets up the environment for the test fixture. |
| ` void` | ` https://developer.android.com/reference/android/test/ProviderTestCase2#tearDown()() ` Tears down the environment for the test fixture. |

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

### ProviderTestCase2

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

```
public ProviderTestCase2 (Class<T> providerClass, 
                String providerAuthority)
```

Constructor.

<br />

| Parameters ||
|---|---|
| `providerClass` | `Class`: The class name of the provider under test <br /> |
| `providerAuthority` | `String`: The provider's authority string <br /> |

## Public methods

### getMockContentResolver

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

```
public MockContentResolver getMockContentResolver ()
```

Gets the `https://developer.android.com/reference/android/test/mock/MockContentResolver` created by this class during initialization. You must use the methods of this resolver to access the provider under test.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/test/mock/MockContentResolver` | A `https://developer.android.com/reference/android/test/mock/MockContentResolver` instance. <br /> |

### getMockContext

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

```
public IsolatedContext getMockContext ()
```

Gets the `https://developer.android.com/reference/android/test/IsolatedContext` created by this class during initialization.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/test/IsolatedContext` | The `https://developer.android.com/reference/android/test/IsolatedContext` instance <br /> |

### getProvider

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

```
public T getProvider ()
```

Returns the content provider created by this class in the `https://developer.android.com/reference/android/test/ProviderTestCase2#setUp()` method.

<br />

| Returns ||
|---|---|
| `T` | T An instance of the provider class given as a parameter to the test case class. <br /> |

### newResolverWithContentProviderFromSql

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

```
public static ContentResolver newResolverWithContentProviderFromSql (Context targetContext, 
                String filenamePrefix, 
                Class<T> providerClass, 
                String authority, 
                String databaseName, 
                int databaseVersion, 
                String sql)
```

<br />

Creates a new content provider of the same type as that passed to the test case class, with an authority name set to the authority parameter, and using an SQLite database as the underlying data source. The SQL statement parameter is used to create the database. This method also creates a new `https://developer.android.com/reference/android/test/mock/MockContentResolver` and adds the provider to it.

Both the new provider and the new resolver are put into an `https://developer.android.com/reference/android/test/IsolatedContext` that uses the targetContext parameter for file operations and a `https://developer.android.com/reference/android/test/mock/MockContext` for everything else. The IsolatedContext prepends the filenamePrefix parameter to file, database, and directory names.

This is a convenience method for creating a "mock" provider that can contain test data.

<br />

<br />

| Parameters ||
|---|---|
| `targetContext` | `Context`: The context to use as the basis of the IsolatedContext <br /> |
| `filenamePrefix` | `String`: A string that is prepended to file, database, and directory names <br /> |
| `providerClass` | `Class`: The type of the provider being tested <br /> |
| `authority` | `String`: The authority string to associated with the test provider <br /> |
| `databaseName` | `String`: The name assigned to the database <br /> |
| `databaseVersion` | `int`: The version assigned to the database <br /> |
| `sql` | `String`: A string containing the SQL statements that are needed to create the desired database and its tables. The format is the same as that generated by the [sqlite3](http://www.sqlite.org/sqlite.html) tool's `.dump` command. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/content/ContentResolver` | ContentResolver A new `https://developer.android.com/reference/android/test/mock/MockContentResolver` linked to the provider <br /> |

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

## Protected methods

### setUp

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

```
protected void setUp ()
```

Sets up the environment for the test fixture.

Creates a new `https://developer.android.com/reference/android/test/mock/MockContentResolver`, a new IsolatedContext that isolates the provider's file operations, and a new instance of the provider under test within the isolated environment.

<br />

<br />

| Throws ||
|---|---|
|   | java.lang.Exception |
| `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 ()
```

Tears down the environment for the test fixture.

Calls `https://developer.android.com/reference/android/content/ContentProvider#shutdown()` on the `https://developer.android.com/reference/android/content/ContentProvider` represented by mProvider.

<br />

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