Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

MockContentResolver

public class MockContentResolver
extends ContentResolver

java.lang.Object
   ↳ android.content.ContentResolver
     ↳ android.test.mock.MockContentResolver


An extension of ContentResolver that is designed for testing.

MockContentResolver overrides Android's normal way of resolving providers by authority. To have access to a provider based on its authority, users of MockContentResolver first instantiate the provider and use addProvider(String, ContentProvider). Resolution of an authority occurs entirely within MockContentResolver.

Users can also set an authority's entry in the map to null, so that a provider is completely mocked out.

Developer Guides

For more information about application testing, read the Testing developer guide.

Summary

Inherited constants

Public constructors

MockContentResolver()

Creates a local map of providers.

MockContentResolver(Context context)

Creates a local map of providers.

Public methods

void addProvider(String name, ContentProvider provider)

Adds access to a provider based on its authority

void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork)

Overrides ContentResolver.notifChange(Uri, ContentObserver, boolean).

Inherited methods

Public constructors

MockContentResolver

added in API level 1
public MockContentResolver ()

Creates a local map of providers. This map is used instead of the global map when an API call tries to acquire a provider.

MockContentResolver

added in API level 18
public MockContentResolver (Context context)

Creates a local map of providers. This map is used instead of the global map when an API call tries to acquire a provider.

Parameters
context Context

Public methods

addProvider

added in API level 1
public void addProvider (String name, 
                ContentProvider provider)

Adds access to a provider based on its authority

Parameters
name String: The authority name associated with the provider.

provider ContentProvider: An instance of ContentProvider or one of its subclasses, or null.

notifyChange

added in API level 1
public void notifyChange (Uri uri, 
                ContentObserver observer, 
                boolean syncToNetwork)

Overrides ContentResolver.notifChange(Uri, ContentObserver, boolean). All parameters are ignored. The method hides providers linked to MockContentResolver from other observers in the system.

Parameters
uri Uri: (Ignored) The uri of the content provider.

observer ContentObserver: (Ignored) The observer that originated the change.

syncToNetwork boolean: (Ignored) If true, attempt to sync the change to the network.