# ProviderTestRule

Artifact: [androidx.test:rules](https://developer.android.com/jetpack/androidx/releases/test) [View Source](https://cs.android.com/search?q=file:androidx/test/rule/provider/ProviderTestRule.java+class:androidx.test.rule.provider.ProviderTestRule)

*** ** * ** ***


```
class ProviderTestRule : TestRule
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> relies on deprecated android.test.mock. Consider using a real provider, or implementing a fake ContentProvider specific to your use case.

A `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestRule.html` to test `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 `https://developer.android.com/reference/kotlin/androidx/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 `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder#setDatabaseFile(java.lang.String,java.io.File)`, `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommands(java.lang.String,java.lang.String...)`, `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommandsFile(java.lang.String,java.io.File)` and `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#runDatabaseCommands(java.lang.String,java.lang.String...)` should only be used when ContentProvider under test is implemented based on `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 `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder#setDatabaseFile(java.lang.String,java.io.File)`
2. Run database commands passed in via `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommands(java.lang.String,java.lang.String...)`
3. Run database commands from file passed in via `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder#setDatabaseCommandsFile(java.lang.String,java.io.File)`

If the `https://developer.android.com/reference/android/content/ContentProvider.html` under test is not implemented based on `https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html`, or is implemented based on `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 `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder`.

Usage example:

```kotlin
@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 `https://developer.android.com/reference/android/content/ContentProvider.html` under test is based on `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 `https://developer.android.com/reference/android/content/ContentProvider.html` under test.

Usage example:

```kotlin
@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 resolver = mProviderRule.getResolver();
    // two entries are already inserted by rule, we can directly perform assertions to verify
    Cursor c = null;
    try {
      c = resolver.query(URI_TO_QUERY_ALL, null, null, null, null);
      assertNotNull(c);
      assertEquals(2, c.getCount());
    } finally {
      if (c != null && !c.isClosed()) {
        c.close();
      }
    }
}
```

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule.Builder` **This class is deprecated.** Consider using a real provider, or implementing a fake ContentProvider specific to your use case. <br /> |

| ### Public functions |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!` | `[apply](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))(base: https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!, description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[revokePermission](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#revokePermission(java.lang.String))(permission: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[runDatabaseCommands](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#runDatabaseCommands(java.lang.String,java.lang.String...))(dbName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, dbCmds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>)` **This function is deprecated.** Deprecated in Java |

| ### Protected functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[afterProviderCleanedUp](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#afterProviderCleanedUp())()` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[beforeProviderSetup](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#beforeProviderSetup())()` **This function is deprecated.** Deprecated in Java |

| ### Public properties |
|---|---|
| `https://developer.android.com/reference/android/content/ContentResolver.html!` | `https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#resolver()` **This property is deprecated.** Deprecated in Java |

## Public functions

### apply

```
fun [apply](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))(base: Statement!, description: Description!): Statement!
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

### revokePermission

```
fun [revokePermission](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#revokePermission(java.lang.String))(permission: String): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

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

- `https://developer.android.com/reference/android/content/Context.html#checkPermission(java.lang.String, int, int)`
- `https://developer.android.com/reference/android/content/Context.html#checkCallingPermission(java.lang.String)`
- `https://developer.android.com/reference/android/content/Context.html#checkSelfPermission(java.lang.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 `https://developer.android.com/reference/java/lang/SecurityException.html`.

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

### runDatabaseCommands

```
fun [runDatabaseCommands](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#runDatabaseCommands(java.lang.String,java.lang.String...))(dbName: String, dbCmds: Array<String!>): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

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

| Parameters |
|---|---|
| `dbName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The name of the underlying database used by the ContentProvider under test. |
| `dbCmds: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>` | The SQL commands to run during tests. Each command will be passed to `https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL(java.lang.String)` to execute. |

## Protected functions

### afterProviderCleanedUp

```
protected fun [afterProviderCleanedUp](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#afterProviderCleanedUp())(): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

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.sourceforge.net/javadoc/org/junit/After.html).

### beforeProviderSetup

```
protected fun [beforeProviderSetup](https://developer.android.com/reference/kotlin/androidx/test/rule/provider/ProviderTestRule#beforeProviderSetup())(): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

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.sourceforge.net/javadoc/org/junit/Before.html).

## Public properties

### resolver

```
val resolver: ContentResolver!
```

> [!CAUTION]
> **This property is deprecated.**   
> Deprecated in Java