[Android test] content provider Test

Source: Internet
Author: User

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 classProviderTestCase2Allows you to test the content provider in an independent environment. Android simulated objects suchIsolatedContextAndMockContentResolverIt also helps provide an isolated test environment.

Similar to other android tests, the content provider test package isInstrumentationTestRunnerUnder 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 useProviderTestCase2To test the content provider. This class inherits fromAndroidTestCaseTherefore, 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 inProviderTestCase2Class constructor will call it in its own constructor.ProviderTestCase2Class constructor createsIsolatedContextObject, 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 createMockContentResolverThe resolver object used for testing.MockContentResolverClass in mock
Objectclasses is described in detail.

Finally, the constructor creates an object for the content provider to be tested. This is normal.ContentProviderObject, but all its system information comes fromIsolatedContextObject, so it is restricted in an isolated test environment. Test
All test items in the case class run on isolated simulated objects.

Simulated Object

ProviderTestCase2UseIsolatedContextAndMockContentResolverThese 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 canProviderTestCase2Class 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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.