Home | History | Annotate | Download | only in testing
      1 page.title=Service 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">Service Design and Testing</a>
     10     </li>
     11     <li>
     12         <a href="#ServiceTestCase">ServiceTestCase</a>
     13     </li>
     14     <li>
     15         <a href="#MockObjects">Mock object classes</a>
     16     </li>
     17     <li>
     18         <a href="#TestAreas">What to Test</a>
     19     </li>
     20   </ol>
     21   <h2>Key Classes</h2>
     22     <ol>
     23       <li>{@link android.test.InstrumentationTestRunner}</li>
     24       <li>{@link android.test.ServiceTestCase}</li>
     25       <li>{@link android.test.mock.MockApplication}</li>
     26       <li>{@link android.test.RenamingDelegatingContext}</li>
     27     </ol>
     28   <h2>Related Tutorials</h2>
     29     <ol>
     30         <li>
     31             <a href="{@docRoot}resources/tutorials/testing/helloandroid_test.html">
     32             Hello, Testing</a>
     33         </li>
     34         <li>
     35             <a href="{@docRoot}resources/tutorials/testing/activity_test.html">Activity Testing</a>
     36         </li>
     37     </ol>
     38   <h2>See Also</h2>
     39       <ol>
     40         <li>
     41           <a href="{@docRoot}guide/developing/testing/testing_eclipse.html">
     42           Testing in Eclipse, with ADT</a>
     43         </li>
     44         <li>
     45           <a href="{@docRoot}guide/developing/testing/testing_otheride.html">
     46           Testing in Other IDEs</a>
     47         </li>
     48       </ol>
     49   </div>
     50 </div>
     51 <p>
     52     Android provides a testing framework for Service objects that can run them in
     53     isolation and provides mock objects. The test case class for Service objects is
     54     {@link android.test.ServiceTestCase}. Since the Service class assumes that it is separate
     55     from its clients, you can test a Service object without using instrumentation.
     56 </p>
     57 <p>
     58     This document describes techniques for testing Service objects. If you aren't familiar with the
     59     Service class, please read <a href="{@docRoot}guide/topics/fundamentals.html">
     60     Application Fundamentals</a>. If you aren't familiar with Android testing, please read
     61     <a href="{@docRoot}guide/topics/testing/testing_android.html">Testing Fundamentals</a>,
     62     the introduction to the Android testing and instrumentation framework.
     63 </p>
     64 <h2 id="DesignAndTest">Service Design and Testing</h2>
     65 <p>
     66     When you design a Service, you should consider how your tests can examine the various states
     67     of the Service lifecycle. If the lifecycle methods that start up your Service, such as
     68     {@link android.app.Service#onCreate() onCreate()} or
     69     {@link android.app.Service#onStartCommand(Intent, int, int) onStartCommand()} do not normally
     70     set a global variable to indicate that they were successful, you may want to provide such a
     71     variable for testing purposes.
     72 </p>
     73 <p>
     74     Most other testing is facilitated by the methods in the {@link android.test.ServiceTestCase}
     75     test case class. For example, the {@link android.test.ServiceTestCase#getService()} method
     76     returns a handle to the Service under test, which you can test to confirm that the Service is
     77     running even at the end of your tests.
     78 </p>
     79 <h2 id="ServiceTestCase">ServiceTestCase</h2>
     80 <p>
     81     {@link android.test.ServiceTestCase} extends the JUnit {@link junit.framework.TestCase} class
     82     with with methods for testing application permissions and for controlling the application and
     83     Service under test. It also provides mock application and Context objects that isolate your
     84     test from the rest of the system.
     85 </p>
     86 <p>
     87     {@link android.test.ServiceTestCase} defers initialization of the test environment until you
     88     call {@link android.test.ServiceTestCase#startService(Intent) ServiceTestCase.startService()} or
     89     {@link android.test.ServiceTestCase#bindService(Intent) ServiceTestCase.bindService()}. This
     90     allows you to set up your test environment, particularly your mock objects, before the Service
     91     is started.
     92 </p>
     93 <p>
     94     Notice that the parameters to <code>ServiceTestCase.bindService()</code>are different from
     95     those for <code>Service.bindService()</code>. For the <code>ServiceTestCase</code> version,
     96     you only provide an Intent. Instead of returning a boolean,
     97     <code>ServiceTestCase.bindService()</code> returns an object that subclasses
     98     {@link android.os.IBinder}.
     99 </p>
    100 <p>
    101     The {@link android.test.ServiceTestCase#setUp()} method for {@link android.test.ServiceTestCase}
    102     is called before each test. It sets up the test fixture by making a copy of the current system
    103     Context before any test methods touch it. You can retrieve this Context by calling
    104     {@link android.test.ServiceTestCase#getSystemContext()}. If you override this method, you must
    105     call <code>super.setUp()</code> as the first statement in the override.
    106 </p>
    107 <p>
    108     The methods {@link android.test.ServiceTestCase#setApplication(Application) setApplication()}
    109     and {@link android.test.AndroidTestCase#setContext(Context)} setContext()} allow you to set
    110     a mock Context or mock Application (or both) for the Service, before you start it. These mock
    111     objects are described in <a href="#MockObjects">Mock object classes</a>.
    112 </p>
    113 <p>
    114     By default, {@link android.test.ServiceTestCase} runs the test method
    115     {@link android.test.AndroidTestCase#testAndroidTestCaseSetupProperly()}, which asserts that
    116     the base test case class successfully set up a Context before running.
    117 </p>
    118 <h2 id="MockObjects">Mock object classes</h2>
    119 <p>
    120     <code>ServiceTestCase</code> assumes that you will use a mock Context or mock Application
    121     (or both) for the test environment. These objects isolate the test environment from the
    122     rest of the system. If you don't provide your own instances of these objects before you
    123     start the Service, then {@link android.test.ServiceTestCase} will create its own internal
    124     instances and inject them into the Service. You can override this behavior by creating and
    125     injecting your own instances before starting the Service
    126 </p>
    127 <p>
    128     To inject a mock Application object into the Service under test, first create a subclass of
    129     {@link android.test.mock.MockApplication}. <code>MockApplication</code> is a subclass of
    130     {@link android.app.Application} in which all the methods throw an Exception, so to use it
    131     effectively you subclass it and override the methods you need. You then inject it into the
    132     Service with the
    133     {@link android.test.ServiceTestCase#setApplication(Application) setApplication()} method.
    134     This mock object allows you to control the application values that the Service sees, and
    135     isolates it from the real system. In addition, any hidden dependencies your Service has on
    136     its application reveal themselves as exceptions when you run the test.
    137 </p>
    138 <p>
    139     You inject a mock Context into the Service under test with the
    140     {@link android.test.AndroidTestCase#setContext(Context) setContext()} method. The mock
    141     Context classes you can use are described in more detail in
    142     <a href="{@docRoot}guide/topics/testing/testing_android.html#MockObjectClasses">
    143     Testing Fundamentals</a>.
    144 </p>
    145 <h2 id="TestAreas">What to Test</h2>
    146 <p>
    147     The topic <a href="{@docRoot}guide/topics/testing/what_to_test.html">What To Test</a>
    148     lists general considerations for testing Android components.
    149     Here are some specific guidelines for testing a Service:
    150 </p>
    151 <ul>
    152     <li>
    153         Ensure that the {@link android.app.Service#onCreate()} is called in response to
    154         {@link android.content.Context#startService(Intent) Context.startService()} or
    155     {@link android.content.Context#bindService(Intent,ServiceConnection,int) Context.bindService()}.
    156         Similarly, you should ensure that {@link android.app.Service#onDestroy()} is called in
    157         response to {@link android.content.Context#stopService(Intent) Context.stopService()},
    158         {@link android.content.Context#unbindService(ServiceConnection) Context.unbindService()},
    159         {@link android.app.Service#stopSelf()}, or
    160         {@link android.app.Service#stopSelfResult(int) stopSelfResult()}.
    161     </li>
    162     <li>
    163         Test that your Service correctly handles multiple calls from
    164         <code>Context.startService()</code>. Only the first call triggers
    165         <code>Service.onCreate()</code>, but all calls trigger a call to
    166         <code>Service.onStartCommand()</code>.
    167         <p>
    168             In addition, remember that <code>startService()</code> calls don't
    169             nest, so a single call to <code>Context.stopService()</code> or
    170             <code>Service.stopSelf()</code> (but not <code>stopSelf(int)</code>)
    171             will stop the Service. You should test that your Service stops at the correct point.
    172         </p>
    173     </li>
    174     <li>
    175         Test any business logic that your Service implements. Business logic includes checking for
    176         invalid values, financial and arithmetic calculations, and so forth.
    177     </li>
    178 </ul>
    179