page.title=Content Provider Testing parent.title=Testing parent.link=index.html @jd:body

In this document

  1. Content Provider Design and Testing
  2. The Content Provider Testing API
    1. ProviderTestCase2
    2. Mock object classes
  3. What To Test
  4. Next Steps

Key Classes

  1. {@link android.test.InstrumentationTestRunner}
  2. {@link android.test.ProviderTestCase2}
  3. {@link android.test.IsolatedContext}
  4. {@link android.test.mock.MockContentResolver}

Related Tutorials

  1. Activity Testing Tutorial

See Also

  1. Testing Fundamentals
  2. Testing From Eclipse with ADT
  3. Testing From Other IDEs

Content providers, which store and retrieve data and make it accessible across applications, are a key part of the Android API. As an application developer you're allowed to provide your own public providers for use by other applications. If you do, then you should test them using the API you publish.

This document describes how to test public content providers, although the information is also applicable to providers that you keep private to your own application. If you aren't familiar with content providers or the Android testing framework, please read Content Providers, the guide to developing content providers, and Testing Fundamentals, the introduction to the Android testing and instrumentation framework.

Content Provider Design and Testing

In Android, content providers are viewed externally as data APIs that provide tables of data, with their internals hidden from view. A content provider may have many public constants, but it usually has few if any public methods and no public variables. This suggests that you should write your tests based only on the provider's public members. A content provider that is designed like this is offering a contract between itself and its users.

The base test case class for content providers, {@link android.test.ProviderTestCase2}, allows you to test your content provider in an isolated environment. Android mock objects such as {@link android.test.IsolatedContext} and {@link android.test.mock.MockContentResolver} also help provide an isolated test environment.

As with other Android tests, provider test packages are run under the control of the test runner {@link android.test.InstrumentationTestRunner}. The section Running Tests With InstrumentationTestRunner describes the test runner in more detail. The topic Testing From Eclipse with ADT shows you how to run a test package in Eclipse, and the topic Testing From Other IDEs shows you how to run a test package from the command line.

Content Provider Testing API

The main focus of the provider testing API is to provide an isolated testing environment. This ensures that tests always run against data dependencies set explicitly in the test case. It also prevents tests from modifying actual user data. For example, you want to avoid writing a test that fails because there was data left over from a previous test, and you want to avoid adding or deleting contact information in a actual provider.

The test case class and mock object classes for provider testing set up this isolated testing environment for you.

ProviderTestCase2

You test a provider with a subclass of {@link android.test.ProviderTestCase2}. This base class extends {@link android.test.AndroidTestCase}, so it provides the JUnit testing framework as well as Android-specific methods for testing application permissions. The most important feature of this class is its initialization, which creates the isolated test environment.

The initialization is done in the constructor for {@link android.test.ProviderTestCase2}, which subclasses call in their own constructors. The {@link android.test.ProviderTestCase2} constructor creates an {@link android.test.IsolatedContext} object that allows file and database operations but stubs out other interactions with the Android system. The file and database operations themselves take place in a directory that is local to the device or emulator and has a special prefix.

The constructor then creates a {@link android.test.mock.MockContentResolver} to use as the resolver for the test. The {@link android.test.mock.MockContentResolver} class is described in detail in the section Mock object classes.

Lastly, the constructor creates an instance of the provider under test. This is a normal {@link android.content.ContentProvider} object, but it takes all of its environment information from the {@link android.test.IsolatedContext}, so it is restricted to working in the isolated test environment. All of the tests done in the test case class run against this isolated object.

Mock object classes

{@link android.test.ProviderTestCase2} uses {@link android.test.IsolatedContext} and {@link android.test.mock.MockContentResolver}, which are standard mock object classes. To learn more about them, please read Testing Fundamentals.

What To Test

The topic What To Test lists general considerations for testing Android components. Here are some specific guidelines for testing content providers.

Next Steps

To learn how to set up and run tests in Eclipse, please refer to Testing from Eclipse with ADT. If you're not working in Eclipse, refer to Testing From Other IDEs.

If you want a step-by-step introduction to testing activities, try the Activity Testing Tutorial, which guides you through a testing scenario that you develop against an activity-oriented application.