コンテンツに移動

よくアクセスするページ

最近アクセスしたページ

navigation

Testing Your Content Provider

Dependencies and Prerequisites

This lesson teaches you to

  1. Create Integration Tests for Content Providers
  2. What to Test

You should also read

If you are implementing a content provider to store and retrieve data or to make data accessible to other apps, you should test your provider to ensure that it doesn't behave in an unexpected way. This lesson describes how to test public content providers, and is also applicable to providers that you keep private to your own app.

Create Integration Tests for Content Providers

In Android, apps view content providers 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. For this reason, 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.

Content providers let you access actual user data, so it's important to ensure that you test the content provider in an isolated testing environment. This approach allows you to only run against data dependencies set explicitly in the test case. It also means that your tests do not modify actual user data. For example, you should avoid writing a test that fails because there was data left over from a previous test. Similarly, your test should avoid adding or deleting actual contact information in a provider.

To test your content provider in isolation, use the ProviderTestCase2 class. This class allows you to use Android mock object classes such as IsolatedContext and MockContentResolver to access file and database information without affecting the actual user data.

Your integration test should be written as a JUnit 4 test class. To learn more about creating JUnit 4 test classes and using JUnit 4 assertions, see Create a Local Unit Test Class.

To create an integration test for your content provider, you must perform these steps:

How ProviderTestCase2 works

You test a provider with a subclass of ProviderTestCase2. This base class extends 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 ProviderTestCase2, which subclasses call in their own constructors. The ProviderTestCase2 constructor creates an 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 MockContentResolver to use as the resolver for the test.

Lastly, the constructor creates an instance of the provider under test. This is a normal ContentProvider object, but it takes all of its environment information from the 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.

You run integration tests for content providers the same way as instrumented unit tests. To run the integration test for your content provider, follow the steps described in Run Instrumented Unit Tests.

What to Test

Here are some specific guidelines for testing content providers.

このサイトでは、ユーザーが選択したサイトの言語と表示設定を保存する目的で Cookie を使用しています。

Android デベロッパー向けの最新情報やヒントを入手して、Google Play での成功を手に入れましょう。

* 必須

送信しました

WeChat で Google Developers をフォローする

このサイトをで表示しますか?

ページの表示言語としてを選択しましたが、このサイトの言語はに設定されています。

言語設定を変更してこのサイトをで表示しますか?言語設定を変更する場合は、各ページの下にある言語メニューを使用してください。

このクラスには、API レベル 以上が必要です。

API レベル が選択されているため、このドキュメントは非表示になっています。左のナビゲーションの上にあるセレクタを使って、ドキュメントの API レベルを変更できます。

アプリに必要な API レベルを指定する方法について、詳しくは異なるプラットフォーム バージョンのサポートをご覧ください。

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)