Address: http://developer.android.com/tools/testing/contentprovider_testing.html
Content Provider is an important part of the android API. It can be used between applications to save and extract data.
This document describes how to test public content providers. It is also applicable to providers used by developers. If you are not familiar with the content provider or android testing, see content providers and testing.
Fundamentals.
Content Provider Design and Testing
In Android, content providers provide APIs for Data Tables externally, which are hidden inside them. A content provider may have many public constants, but it has few public methods and almost no public variables. This indicates that your test should be based on the public members of the provider, the content provider designed in this way provides a channel for communication between users and users.
Content provider's test case classProviderTestCase2
Allows you to test the content provider in an independent environment. Android simulated objects suchIsolatedContext
AndMockContentResolver
It also helps provide an isolated test environment.
Similar to other android tests, the content provider test package isInstrumentationTestRunner
Under the control,
Running tests with instrumentationtestrunner describes the test runner in detail.
Content Provider API
The focus of the content provider test API is to provide an isolated test environment. The test case class and the simulated object class can help you create an isolated test environment.
Providertesecase2
You can useProviderTestCase2
To test the content provider. This class inherits fromAndroidTestCase
Therefore, it provides both the JUnit test framework and Android-specific methods to test application permissions. The most important feature of this class is that it creates an isolated test environment during initialization.
The initialization is inProviderTestCase2
Class constructor will call it in its own constructor.ProviderTestCase2
Class constructor createsIsolatedContext
Object, which allows file and database operations, but does not allow other interactions with the Android system. File and database operations are performed in a local directory of the device or simulator, this directory has a specific prefix.
Then the constructor will createMockContentResolver
The resolver object used for testing.MockContentResolver
Class in mock
Objectclasses is described in detail.
Finally, the constructor creates an object for the content provider to be tested. This is normal.ContentProvider
Object, but all its system information comes fromIsolatedContext
Object, so it is restricted in an isolated test environment. Test
All test items in the case class run on isolated simulated objects.
Simulated Object
ProviderTestCase2
UseIsolatedContext
AndMockContentResolver
These standard simulated objects.
Test What
What to test lists the common considerations for testing the andorid component. The following are especially considerations for the content provider test:
- Test with resolver: although you can
ProviderTestCase2
Class to instantiate a provider object, but you should use a resolver object with a suitable URI for testing, so that you can test the provider in an interactive way that is usually used by the application.
- Test the common provider as a contract: If you want your provider to be public and accessible to other applications, you should test it as a contract. The method is as follows:
- Use constants exposed by providers for testing. For example, to search for constants that represent the names of data table columns in the provider, these constants should be public constants defined by the provider.
- Test all Uris provided by the provider. Your provider may provide multiple Uris, each pointing to different data. For example, the note pad instance implements a provider. One URI is used to obtain the Notes List, the other is used to obtain the database ID of a single note, and the third is used to display the note in live folder.
- Invalid URI test: Your test should use an invalid URI to call the provider. A well-designed provider should throw an illeagalargumentexception for the invalid Uri.
- Test Standard provider Interaction: Most providers provide six access methods: qurey, insert, delete, update, GetType, and oncreate. Your test should verify that these methods work properly.
- Test business logic: Do not forget the business logic to be executed by the test provider. Business Logic includes processing illegal data, arithmetic operations, elimination or integration of repeated items. The content provider does not necessarily need to have business logic, because the business logic may be implemented by using the data actiivity. If the provider does provide the business logic, you should test it.