# ProviderTestRule

Summary:[Nested Classes](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#nestedclasses)\|[Methods](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#)  

# ProviderTestRule

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
ProviderTestRule
`  
`

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


``


implements

TestRule


`

|---|-----------------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||
| ↳ | android.support.test.rule.provider.ProviderTestRule |

<br />

*** ** * ** ***

A[TestRule](https://developer.android.com/reference/org/junit/rules/TestRule)to test[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider.html)s, with additional APIs to enable easy initialization such as restoring database from a file, running database commands passed in as a String or a file. By default, all permissions are granted when they are checked in ContentProviders under test, method[revokePermission(String)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#revokePermission(java.lang.String))can be used to revoke specific permissions to test the cases when they are denied in ContentProviders.

Note: The database related methods[setDatabaseFile(String, File)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder#setDatabaseFile(java.lang.String, java.io.File)),[setDatabaseCommands(String, String...)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommands(java.lang.String, java.lang.String...)),[setDatabaseCommandsFile(String, File)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommandsFile(java.lang.String, java.io.File))and[runDatabaseCommands(String, String...)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#runDatabaseCommands(java.lang.String, java.lang.String...))should only be used when ContentProvider under test is implemented based on[SQLiteDatabase](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html).

If more than one database related methods are used for a ContentProvider under test, the execution order of restoring database from file and running database commands are independent of the order those methods are called. If all methods are used, when setting up the ContentProvider for test, the execution order is as follows:

1. Restore database from file passed in via[setDatabaseFile(String, File)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder#setDatabaseFile(java.lang.String, java.io.File))
2. Run database commands passed in via[setDatabaseCommands(String, String...)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommands(java.lang.String, java.lang.String...))
3. Run database commands from file passed in via[setDatabaseCommandsFile(String, File)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommandsFile(java.lang.String, java.io.File))

If the[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider.html)under test is not implemented based on[SQLiteDatabase](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html), or is implemented based on[SQLiteDatabase](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html)but no extra database initialization workloads are needed, the rule can be created by simply using[Builder(Class,String)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder).

Usage example:  

```
 @Rule
 public ProviderTestRule mProviderRule =
     new ProviderTestRule.Builder(MyContentProvider.class, MyContentProvider.AUTHORITY).build();

 @Test
 public void verifyContentProviderContractWorks() {
     ContentResolver resolver = mProviderRule.getResolver();
     // perform some database (or other) operations
     Uri uri = resolver.insert(testUrl, testContentValues);
     // perform some assertions on the resulting URI
     assertNotNull(uri);
 }
 
```

Alternatively, if the[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider.html)under test is based on[SQLiteDatabase](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html), then all database related methods can be used. However, the database name argument passed in via these methods must match the actual database name used by the[ContentProvider](https://developer.android.com/reference/android/content/ContentProvider.html)under test.

Usage example:  

```
 @Rule
 public ProviderTestRule mProviderRule =
     new ProviderTestRule.Builder(MyContentProvider.class, MyContentProvider.AUTHORITY)
         .setDatabaseCommands(DATABASE_NAME, INSERT_ONE_ENTRY_CMD, INSERT_ANOTHER_ENTRY_CMD)
         .build();

 @Test
 public void verifyTwoEntriesInserted() {
     ContentResolver mResolver = mProviderRule.getResolver();
     // two entries are already inserted by rule, we can directly perform assertions to verify
     Cursor c = null;
     try {
       c = mResolver.query(URI_TO_QUERY_ALL, null, null, null, null);
       assertNotNull(c);
       assertEquals(2, c.getCount());
     } finally {
       if (c != null && !c.isClosed()) {
         c.close();
       }
     }
 }
 
```

**This API is currently in beta.**

## Summary

|                                                                                                                                 ### Nested classes                                                                                                                                  ||
|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [ProviderTestRule.Builder](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder) A Builder to ease[ProviderTestRule](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule)creation. |

|                                                                                                                                                                                                                                                            ### Public methods                                                                                                                                                                                                                                                             ||
|----------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` Statement`                                                                                       | ` `[apply](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description))`(Statement base, Description description) `                                                                                                                                                                                                            |
| ` `[ContentResolver](https://developer.android.com/reference/android/content/ContentResolver.html) | ` `[getResolver](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#getResolver())`() ` Get the isolated[ContentResolver](https://developer.android.com/reference/android/content/ContentResolver.html)that should be used for testing of the ContentProviders.                                                                                                                              |
| ` void`                                                                                            | ` `[revokePermission](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#revokePermission(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String.html)` permission) ` Revoke permission anytime during the tests.                                                                                                                                            |
| ` void`                                                                                            | ` `[runDatabaseCommands](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#runDatabaseCommands(java.lang.String, java.lang.String...))`(`[String](https://developer.android.com/reference/java/lang/String.html)` dbName, `[String...](https://developer.android.com/reference/java/lang/String.html)` dbCmds) ` Run database commands anytime during the tests, after the rule is created. |

|                                                                                                                 ### Protected methods                                                                                                                  ||
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[afterProviderCleanedUp](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#afterProviderCleanedUp())`() ` Override this method to execute any code that should run after provider is cleaned up. |
| ` void` | ` `[beforeProviderSetup](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#beforeProviderSetup())`() ` Override this method to execute any code that should run before provider is set up.          |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#)From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object.html)` ` |-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object.html)          | ` clone() `                                                                                | | ` boolean`                                                                          | ` equals(`[Object](https://developer.android.com/reference/java/lang/Object.html)` arg0) ` | | ` void`                                                                             | ` finalize() `                                                                             | | ` final `[Class](https://developer.android.com/reference/java/lang/Class.html)`<?>` | ` getClass() `                                                                             | | ` int`                                                                              | ` hashCode() `                                                                             | | ` final void`                                                                       | ` notify() `                                                                               | | ` final void`                                                                       | ` notifyAll() `                                                                            | | ` `[String](https://developer.android.com/reference/java/lang/String.html)          | ` toString() `                                                                             | | ` final void`                                                                       | ` wait(long arg0, int arg1) `                                                              | | ` final void`                                                                       | ` wait(long arg0) `                                                                        | | ` final void`                                                                       | ` wait() `                                                                                 | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule#)From interface` org.junit.rules.TestRule ` |-----------------------|---------------------------------------------| | ` abstract Statement` | ` apply(Statement arg0, Description arg1) ` | ||

## Public methods

### apply

```
Statement apply (Statement base, 
                Description description)
```

<br />

<br />

|             Parameters              ||
|---------------|----------------------|
| `base`        | `Statement` <br />   |
| `description` | `Description` <br /> |

|       Returns       ||
|-------------|--------|
| `Statement` | <br /> |

### getResolver

```
ContentResolver getResolver ()
```

Get the isolated[ContentResolver](https://developer.android.com/reference/android/content/ContentResolver.html)that should be used for testing of the ContentProviders.

<br />

|                                                                                                                        Returns                                                                                                                        ||
|-------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ContentResolver](https://developer.android.com/reference/android/content/ContentResolver.html) | the isolated[ContentResolver](https://developer.android.com/reference/android/content/ContentResolver.html)created by this`ProviderTestRule`. <br /> |

### revokePermission

```
void revokePermission (String permission)
```

Revoke permission anytime during the tests. The default return value of the following methods is`PackageManager.PERMISSION_GRANTED`. After a specific permission is revoked, the value returned becomes`PackageManager.PERMISSION_DENIED`when calling the methods with the revoked permission. After a test, the return values are restored to`PackageManager.PERMISSION_GRANTED`.

- [checkPermission(String, int, int)](https://developer.android.com/reference/android/content/Context.html#checkPermission(java.lang.String, int, int))
- [checkCallingPermission(String)](https://developer.android.com/reference/android/content/Context.html#checkCallingPermission(java.lang.String))
- [checkSelfPermission(String)](https://developer.android.com/reference/android/content/Context.html#checkSelfPermission(java.lang.String))
- [checkCallingOrSelfPermission(String)](https://developer.android.com/reference/android/content/Context.html#checkCallingOrSelfPermission(java.lang.String))

Consequentially, calling the following methods with the revoked permission will throw a[SecurityException](https://developer.android.com/reference/java/lang/SecurityException.html).

- [enforcePermission(String, int, int, String)](https://developer.android.com/reference/android/content/Context.html#enforcePermission(java.lang.String, int, int, java.lang.String))
- [enforceCallingPermission(String, String)](https://developer.android.com/reference/android/content/Context.html#enforceCallingPermission(java.lang.String, java.lang.String))
- [enforceCallingOrSelfPermission(String, String)](https://developer.android.com/reference/android/content/Context.html#enforceCallingOrSelfPermission(java.lang.String, java.lang.String))

<br />

<br />

|          Parameters           ||
|--------------|-----------------|
| `permission` | `String` <br /> |

### runDatabaseCommands

```
void runDatabaseCommands (String dbName, 
                String... dbCmds)
```

Run database commands anytime during the tests, after the rule is created.

<br />

<br />

|                                                                                                                 Parameters                                                                                                                 ||
|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `dbName` | `String`: The name of the underlying database used by the ContentProvider under test. <br />                                                                                                                                     |
| `dbCmds` | `String`: The SQL commands to run during tests. Each command will be passed to[execSQL(String)](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL(java.lang.String))to execute. <br /> |

## Protected methods

### afterProviderCleanedUp

```
void afterProviderCleanedUp ()
```

Override this method to execute any code that should run after provider is cleaned up. This method is called after each test method, including any method annotated with[`After`](http://junit.source.net/javadoc/org/junit/After.html).

<br />

### beforeProviderSetup

```
void beforeProviderSetup ()
```

Override this method to execute any code that should run before provider is set up. This method is called before each test method, including any method annotated with[`Before`](http://junit.source.net/javadoc/org/junit/Before.html).

<br />

-

  ## Classes

  - [ProviderTestRule](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule)
  - [ProviderTestRule.Builder](https://developer.android.com/reference/android/support/test/rule/provider/ProviderTestRule.Builder)