1page.title=Content Provider Testing 2@jd:body 3 4<div id="qv-wrapper"> 5 <div id="qv"> 6 <h2>In this document</h2> 7 <ol> 8 <li> 9 <a href="#DesignAndTest">Content Provider Design and Testing</a> 10 </li> 11 <li> 12 <a href="#ContentProviderTestAPI">The Content Provider Testing API</a> 13 <ol> 14 <li> 15 <a href="#ProviderTestCase2">ProviderTestCase2 </a> 16 </li> 17 <li> 18 <a href="#MockObjects">Mock object classes</a> 19 </li> 20 </ol> 21 </li> 22 <li> 23 <a href="#WhatToTest">What To Test</a> 24 </li> 25 <li> 26 <a href="#NextSteps">Next Steps</a> 27 </li> 28 </ol> 29 <h2>Key Classes</h2> 30 <ol> 31 <li>{@link android.test.InstrumentationTestRunner}</li> 32 <li>{@link android.test.ProviderTestCase2}</li> 33 <li>{@link android.test.IsolatedContext}</li> 34 <li>{@link android.test.mock.MockContentResolver}</li> 35 </ol> 36 <h2>See Also</h2> 37 <ol> 38 <li> 39 <a 40 href="{@docRoot}guide/topics/testing/testing_android.html"> 41 Testing Fundamentals</a> 42 </li> 43 <li> 44 <a href="{@docRoot}guide/developing/testing/testing_eclipse.html"> 45 Testing in Eclipse, with ADT</a> 46 </li> 47 <li> 48 <a href="{@docRoot}guide/developing/testing/testing_otheride.html"> 49 Testing in Other IDEs</a> 50 </li> 51 </ol> 52 </div> 53</div> 54<p> 55 Content providers, which store and retrieve data and make it accessible across applications, 56 are a key part of the Android API. As an application developer you're allowed to provide your 57 own public providers for use by other applications. If you do, then you should test them 58 using the API you publish. 59</p> 60<p> 61 This document describes how to test public content providers, although the information is 62 also applicable to providers that you keep private to your own application. If you aren't 63 familiar with content providers or the Android testing framework, please read 64 <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>, 65 the guide to developing content providers, and 66 <a href="{@docRoot}guide/topics/testing/testing_android.html">Testing Fundamentals</a>, 67 the introduction to the Android testing and instrumentation framework. 68</p> 69<h2 id="DesignAndTest">Content Provider Design and Testing</h2> 70<p> 71 In Android, content providers are viewed externally as data APIs that provide 72 tables of data, with their internals hidden from view. A content provider may have many 73 public constants, but it usually has few if any public methods and no public variables. 74 This suggests that you should write your tests based only on the provider's public members. 75 A content provider that is designed like this is offering a contract between itself and its 76 users. 77</p> 78<p> 79 The base test case class for content providers, 80 {@link android.test.ProviderTestCase2}, allows you to test your content provider in an 81 isolated environment. Android mock objects such as {@link android.test.IsolatedContext} and 82 {@link android.test.mock.MockContentResolver} also help provide an isolated test environment. 83</p> 84<p> 85 As with other Android tests, provider test packages are run under the control of the test 86 runner {@link android.test.InstrumentationTestRunner}. The section 87 <a href="{@docRoot}guide/topics/testing/testing_android.html#InstrumentationTestRunner"> 88 Running Tests With InstrumentationTestRunner</a> describes the test runner in 89 more detail. The topic <a href="{@docRoot}guide/developing/testing/testing_eclipse.html"> 90 Testing in Eclipse, with ADT</a> shows you how to run a test package in Eclipse, and the 91 topic <a href="{@docRoot}guide/developing/testing/testing_otheride.html"> 92 Testing in Other IDEs</a> 93 shows you how to run a test package from the command line. 94</p> 95<h2 id="ContentProviderTestAPI">Content Provider Testing API</h2> 96<p> 97 The main focus of the provider testing API is to provide an isolated testing environment. This 98 ensures that tests always run against data dependencies set explicitly in the test case. It 99 also prevents tests from modifying actual user data. For example, you want to avoid writing 100 a test that fails because there was data left over from a previous test, and you want to 101 avoid adding or deleting contact information in a actual provider. 102</p> 103<p> 104 The test case class and mock object classes for provider testing set up this isolated testing 105 environment for you. 106</p> 107<h3 id="ProviderTestCase2">ProviderTestCase2</h3> 108<p> 109 You test a provider with a subclass of {@link android.test.ProviderTestCase2}. This base class 110 extends {@link android.test.AndroidTestCase}, so it provides the JUnit testing framework as well 111 as Android-specific methods for testing application permissions. The most important 112 feature of this class is its initialization, which creates the isolated test environment. 113</p> 114<p> 115 The initialization is done in the constructor for {@link android.test.ProviderTestCase2}, which 116 subclasses call in their own constructors. The {@link android.test.ProviderTestCase2} 117 constructor creates an {@link android.test.IsolatedContext} object that allows file and 118 database operations but stubs out other interactions with the Android system. 119 The file and database operations themselves take place in a directory that is local to the 120 device or emulator and has a special prefix. 121</p> 122<p> 123 The constructor then creates a {@link android.test.mock.MockContentResolver} to use as the 124 resolver for the test. The {@link android.test.mock.MockContentResolver} class is described in 125 detail in the section 126 <a href="{@docRoot}guide/topics/testing/testing_android.html#MockObjectClasses">Mock object 127classes</a>. 128</p> 129<p> 130 Lastly, the constructor creates an instance of the provider under test. This is a normal 131 {@link android.content.ContentProvider} object, but it takes all of its environment information 132 from the {@link android.test.IsolatedContext}, so it is restricted to 133 working in the isolated test environment. All of the tests done in the test case class run 134 against this isolated object. 135</p> 136<h3 id="MockObjects">Mock object classes</h3> 137<p> 138 {@link android.test.ProviderTestCase2} uses {@link android.test.IsolatedContext} and 139 {@link android.test.mock.MockContentResolver}, which are standard mock object classes. To 140 learn more about them, please read 141 <a href="{@docRoot}guide/topics/testing/testing_android.html#MockObjectClasses"> 142 Testing Fundamentals</a>. 143</p> 144<h2 id="WhatToTest">What To Test</h2> 145<p> 146 The topic <a href="{@docRoot}guide/topics/testing/what_to_test.html">What To Test</a> 147 lists general considerations for testing Android components. 148 Here are some specific guidelines for testing content providers. 149</p> 150<ul> 151 <li> 152 Test with resolver methods: Even though you can instantiate a provider object in 153 {@link android.test.ProviderTestCase2}, you should always test with a resolver object 154 using the appropriate URI. This ensures that you are testing the provider using the same 155 interaction that a regular application would use. 156 </li> 157 <li> 158 Test a public provider as a contract: If you intent your provider to be public and 159 available to other applications, you should test it as a contract. This includes 160 the following ideas: 161 <ul> 162 <li> 163 Test with constants that your provider publicly exposes. For 164 example, look for constants that refer to column names in one of the provider's 165 data tables. These should always be constants publicly defined by the provider. 166 </li> 167 <li> 168 Test all the URIs offered by your provider. Your provider may offer several URIs, 169 each one referring to a different aspect of the data. The 170 <a href="{@docRoot}resources/samples/NotePad/index.html">Note Pad</a> sample, 171 for example, features a provider that offers one URI for retrieving a list of notes, 172 another for retrieving an individual note by it's database ID, and a third for 173 displaying notes in a live folder. 174 </li> 175 <li> 176 Test invalid URIs: Your unit tests should deliberately call the provider with an 177 invalid URI, and look for errors. Good provider design is to throw an 178 IllegalArgumentException for invalid URIs. 179 180 </li> 181 </ul> 182 </li> 183 <li> 184 Test the standard provider interactions: Most providers offer six access methods: 185 query, insert, delete, update, getType, and onCreate(). Your tests should verify that all 186 of these methods work. These are described in more detail in the topic 187 <a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>. 188 </li> 189 <li> 190 Test business logic: Don't forget to test the business logic that your provider should 191 enforce. Business logic includes handling of invalid values, financial or arithmetic 192 calculations, elimination or combining of duplicates, and so forth. A content provider 193 does not have to have business logic, because it may be implemented by activities that 194 modify the data. If the provider does implement business logic, you should test it. 195 </li> 196</ul> 197<h2 id="NextSteps">Next Steps</h2> 198<p> 199 To learn how to set up and run tests in Eclipse, please refer to <a 200 href="{@docRoot}guide/developing/testing/testing_eclipse.html">Testing in 201 Eclipse, with ADT</a>. If you're not working in Eclipse, refer to <a 202 href="{@docRoot}guide/developing/testing/testing_otheride.html">Testing in Other 203 IDEs</a>. 204</p> 205<p> 206 If you want a step-by-step introduction to testing activities, try one of the 207 testing tutorials: 208</p> 209<ul> 210 <li> 211 The <a 212 href="{@docRoot}resources/tutorials/testing/helloandroid_test.html">Hello, 213 Testing</a> tutorial introduces basic testing concepts and procedures in the 214 context of the Hello, World application. 215 </li> 216 <li> 217 The <a 218 href="{@docRoot}resources/tutorials/testing/activity_test.html">Activity 219 Testing</a> tutorial is an excellent follow-up to the Hello, Testing tutorial. 220 It guides you through a more complex testing scenario that you develop against a 221 more realistic activity-oriented application. 222 </li> 223</ul> 224